You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET SharpZipLib压缩数据在PHP中解压恢复原文件方案咨询

How to Restore Original Files from SharpZipLib-compressed Base64 Data in 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=zip is uncommented in your php.ini file (restart your web server after making changes).
  • Entry Name Matching: Your VB.NET code creates a ZipEntry with 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:48:31