问题
I want to modify some files inside zip file. I tried using the class zip and it works:
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
$zip->extractTo('./', 'test.txt');
$fp=fopen("test.txt","a");
fputs($fp,"Hello"."\n");
fclose($fp);
$zip->addFile('test.txt');
$zip->close();
unlink ("test.txt");
}
Is it possible to not create temp file for modify, extract, repack, etc. Instead, only modify inside of zip file the target file I want change.
If it is not possible with zip files no problem. I can use other compression file formats, such as tar, etc. I need some format let me compress many files inside and modify this in his contents.
回答1:
If you compress files (and thus change their size within the container), then regular archive formats won't let you modify the files in-place, because any modification will cause size change and the file won't fit well.
Filesystems that support per-file compression (NTFS, our SolFS etc) do this by compressing several blocks (sectors, pages) of the file and trying to put them to smaller number of blocks. Also filesystems usually have extra free space. So modification of the file, when it leads to change of the file size, causes blocks to be written to different place other than the original sectors of the disk.
This said, no compression file format will work for you. TAR is not a compression format (i.e. files are stored uncompressed) but again if you modify the file and this causes size change, TAR won't help.
The only option I can see is a filesystem in a file (eg. Codebase Filesystem, our SolFS) that supports compression. Unfortunately neither Codebase filesystem nor SolFS have PHP interface.
来源:https://stackoverflow.com/questions/23968149/modify-files-inside-zip-file-other-way