0

I'm attempting to read a zip file in PHP that I know has a CRC error. Unfortunately, it looks like I can only get 31 bytes of the file to read. Using just $zip->getFromName() gives no errors but just reads 31 bytes

$zip = new ZipArchive;
$zip->open("path/to/corrupted.zip");

// $contents will become just 31 bytes
$contents = $zip->getFromName("file/in/zip.txt");

Trying to read from the stream $zip->getStream() will give a CRC error, and again will only read 31 bytes

$fp = $zip->getStream("file/in/zip.txt");

$contents = "";
while (!feof($fp)) {
    // Gives the error "fread(): Zip stream error: CRC error in ..."
    // Using 1 instead of 2 still only reads 31 bytes but gives no error
    $contents .= fread($fp, 2);
}

fclose($fp);

So, is there any way I could ignore this CRC error, and read the file anyways?

A little background: my website pre-processes .jar files users upload before they're downloaded by others. Users will occasionally upload a valid .jar file with CRC errors in an attempt to deter people from decompiling it. However, I still want to be able to pre-process these files for download.

I have a different pre-processor written in Python, and I was able to pretty easily disable the CRC check by modifying the zipfile library and commenting out the line that does the crc check. Is there any easy way I could do a similar thing in PHP without needing to make my own ZipFile library? I suppose worst case that's what I'll need to do.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • Unrelated: [Why `while(!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Barmar Aug 12 '22 at 20:17
  • To answer your question: I doubt it. Perhaps you could call out to Python so you can use the modified Python library. – Barmar Aug 12 '22 at 20:18

1 Answers1

0

You can switch to a pure php zip library, so you can remove the crc check without having to rewrite the whole library on your own, and you also haven't to compile native code. Here is an example: https://www.phpclasses.org/package/3864-PHP-Create-and-extract-ZIP-archives-in-purely-in-PHP.html

Daniels118
  • 1,149
  • 1
  • 8
  • 17