Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File separators of Path name of ZipEntry?

ZIP entries store the full path name of the entry because (I'm sure of the next part) the ZIP archive is not organized as directories. The metadata contains the info about how files are supposed to be stored (inside directories).

If I create a ZIP file in Windows, when I unzip the data in another OS, e.g. Mac OS X, the file structure remains as it used to be in Windows. Is this because the unzipper is designed to handle this, or isit because the file separators inside the ZIP are standard?

I'm asking this because I'm trying to find an entry inside a ZIP file using the name of the zipped file. But which file separator should I use to make it work in systems other than Windows?

I'm using Java, and the method: .getName() of the ZipEntry gives me the path using the Windows file separator \. Would it be enough if I use the java File.separator separator to make it work on another OS? Or will I have to try to find my file with each possible separator?

Honorary Correct Answer Mention

The answer given by @Eren Yilmaz is correct describing the functionality of many tools (or even the one you can code yourself). But given that the .zip standard clearly documents how it must be, the correct answer had to be updated

like image 449
Ordiel Avatar asked Dec 12 '12 18:12

Ordiel


People also ask

What is file path separator?

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

Which character is used as a path separator?

pathSeparator would be ; . File. separator is either / or \ that is used to split up the path to a specific file. For example on Windows it is \ or C:\Documents\Test.

What is path separator in Java?

For file path or directory separator, the Unix system introduced the slash character / as directory separator, and the Microsoft Windows introduced backslash character \ as the directory separator. In a nutshell, this is / on UNIX and \ on Windows.

How do I zip a file in Golang?

To zip a file or a directory in Go using the standard library, use zip. Writer type from the archive/zip package. Creating a compressed archive using this method involves going through all the files you want to include, generating a local file header for each one, and writing its contents to the resulting ZIP file.


2 Answers

The .zip file specification states:

4.4.17.1 The name of the file, with optional relative path. The path stored MUST not contain a drive or device letter, or a leading slash. All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility with Amiga and UNIX file systems etc. If input came from standard input, there is no file name field.

like image 108
Ed Randall Avatar answered Oct 11 '22 10:10

Ed Randall


The file separator is dependent on the application that creates the zip file. Some applications use the system file separator, whereas some use the "civilized" forward slash "/". So, if you are creating the zip file and then consuming it, then you can simply use a forward slash as file separator. If the zip file is created on somewhere else, then you should find out which separator was used. I don't know a simple way, but you can use a brute method and check out both separator types as you progress.

Some applications, especially custom zip creation codes, can mix the separators on different zip entries, so don't forget to check out each entry.

like image 21
Eren Yilmaz Avatar answered Oct 11 '22 12:10

Eren Yilmaz