VB.NET SharpZipLib压缩数据在PHP中解压恢复原文件方案咨询
Alright, let's tackle this problem. Your VB.NET code uses SharpZipLib to create a ZIP archive containing a single entry with an empty filename, then compresses your input data (like images) into that entry. The compressed data is stored as Base64 in your database, and you've already handled decoding the Base64 string into binary data. Here's how to extract the original file in PHP:
Step-by-Step Implementation
We'll use PHP's built-in ZipArchive extension (make sure it's enabled in your php.ini) to parse the ZIP data and extract the content of the empty-named entry.
1. Decode the Base64 String
First, convert the stored Base64 text back to binary ZIP data:
// Replace this with your actual Base64 string from the database $base64CompressedData = 'YOUR_BASE64_STRING_HERE'; $zipBinaryData = base64_decode($base64CompressedData);
2. Extract the Original Data from the ZIP
You can use either a temporary file or an in-memory stream to process the ZIP data. Here's both approaches:
Option 1: Using a Temporary File (Simpler, More Compatible)
// Create a temporary file to hold the ZIP data $tempZipPath = tempnam(sys_get_temp_dir(), 'sharpziplib_'); file_put_contents($tempZipPath, $zipBinaryData); $zip = new ZipArchive(); if ($zip->open($tempZipPath) === true) { // Locate the entry with an empty filename (matches your VB.NET code) $entryIndex = $zip->locateName(''); if ($entryIndex !== false) { // Extract the raw original data $originalFileData = $zip->getFromIndex($entryIndex); // Example: Save the original image to a file file_put_contents('restored_image.jpg', $originalFileData); echo "Original file restored successfully!"; } else { throw new Exception("Could not find the empty-named ZIP entry (matches VB.NET's ZipEntry setup)"); } $zip->close(); } else { throw new Exception("Failed to open the ZIP data - it may be corrupted"); } // Clean up the temporary file unlink($tempZipPath);
Option 2: Using an In-Memory Stream (No Temporary File)
If you prefer avoiding disk I/O, you can use PHP's php://temp stream:
$zipStream = fopen('php://temp', 'r+'); fwrite($zipStream, $zipBinaryData); rewind($zipStream); $zip = new ZipArchive(); // Open the stream using its file descriptor if ($zip->open('php://fd/' . (int)$zipStream) === true) { $entryIndex = $zip->locateName(''); if ($entryIndex !== false) { $originalFileData = $zip->getFromIndex($entryIndex); // Use or save $originalFileData as needed file_put_contents('restored_file.png', $originalFileData); } else { throw new Exception("Empty-named ZIP entry not found"); } $zip->close(); } else { throw new Exception("Failed to process ZIP data from memory"); } fclose($zipStream);
Key Notes
- Enable ZipArchive Extension: Ensure
extension=zipis uncommented in yourphp.inifile (restart your web server after making changes). - Entry Name Matching: Your VB.NET code creates a
ZipEntrywith an empty string as the name, so we explicitly look for''in PHP. - Error Handling: Add try/catch blocks around this code to handle edge cases like corrupted ZIP data or missing entries in production.
内容的提问来源于stack exchange,提问作者Tiziano

