Control the sort order of files in a zip archive

Description

I am interested to know if there is a way to control the sort order of files inside zip files using standard procedures in PHP and / or Java.

I am not interested in using zip / unzip with shell_exec()or similar, but this may be of interest if it contains an easy to read solution.

In sort order, we can assume that this means date / time if there is no sort order in the zip file. I have not read the specifications, so I do not know.

Example

Files

foo.txt
bar.txt
test.txt
newfile.txt

Suppose each file contains a file name (foo.txt => foo.txt)

Problem

, unzip . , ? unzip -p zip .

, , .

( ( unzip -p))

test.txt
foo.txt
newfile.txt
bar.txt

+3
3

zipinfo ( unzip -Z) . man zipinfo , , PHP, .

0

. , , , unzip -p, , , 0...n.

, . (, txt )

$files = array(
    'test.txt',
    'foo.txt',
    'newfile.txt',
    'bar.txt'
);

$outfile = 'testout.zip';
if (file_exists($outfile)) {
    unlink($outfile);
}
$o = new ZipArchive();
$o->open($outfile,ZipArchive::CREATE);
foreach($files as $key => $file) {
    $o->addFile($file);
}

$o->close();
+4

The sort order of the unpacked files (or any files in the file system) depends on the "client" that you use to view the files. The ls command on the * nix system will display files sorted by name by default. If you use any form of file manager application (or unzipper), you can choose sorting. The predefined sorting in the archive does not make much sense to me.

0
source

Source: https://habr.com/ru/post/1727198/


All Articles