0

Is there a way to zip a file to include absolute paths in Linux via the command line?

I have a Python program that checks zips and tars for absolute paths, and would like to create a zip file to check if the function works.

I was able to successfully do this with a tar. I have also Googled far and wide for an answer to this and checked the zip documentation and manual, but to haven't found a clear answer.

  • Do you mean with the original absolute path names in the archive as metadata? – Guy Gastineau Mar 20 at 4:33
  • Yes, for instance /home/user/test.txt. – BrianRT Mar 20 at 15:15
1
$> zip file.zip $PWD/test.txt
adding: path/to/test.txt (deflated 5%)
$> unzip -l file.zip
Archive:  file.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       20  03-11-2019 21:32   path/to/test.txt
---------                     -------
       20                     1 file

There is also a switch for that :

$> zip -jj file.zip test.txt
adding: test.txt (deflated 5%)
$> unzip -l file.zip 
Archive:  file.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       20  03-11-2019 21:32   path/to/test.txt
---------                     -------
       20                     1 files

It gives you the exact same result, but if you it to start with / then you can't really, but you can specify that when you uncompress with -d option.

Yeah! that's an edit, It came to my mind maybe your're talking about gz, xz, bzip2? If so, you need to arhive them first, these don't have concept of filesystem like tar does. zip, rar, 7z, all of them do something like that.

1

In the ZIP file format, the file paths are always relative, see for instance this:

  file name: (Variable)

      The name of the file, with optional relative path.
      The path stored should not contain a drive or
      device letter, or a leading slash.  All slashes
      should be forward slashes '/' as opposed to
      backwards slashes '\' for compatibility with Amiga
      and Unix file systems etc.

Of course it is possible to patch up a ZIP file to force a leading slash, but all regular apps are normally protected against such obvious hacks(*).

(*) an even more annoying hack was the file of near-infinite length made of consecutive 0's, that encodes in a handful of bytes. Decompressing that would often take your machine down. All ZIP-handling utilities will detect this now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.