在内存中从多个文件创建zip

5

我正在尝试使用SharpZipLib生成zip文件并让客户端下载。

目前,zip文件夹已经创建并且可以在客户端机器上找到,但问题是download.zip文件夹是空的。指定文件夹中指定的文件在zip文件夹中不可用。

下面是我尝试过的代码。

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();

//stream directly to client.
ICSharpCode.SharpZipLib.Zip.ZipOutputStream output = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(response.OutputStream);
output.SetLevel(9);
string[] files =    Directory.GetFiles("D:/newfolder/");
for (int i = 0; i < files.Length; i++)
{
    ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(files[i].ToString());
    entry.DateTime = DateTime.Now;
    System.IO.FileStream fs = new System.IO.FileStream(files[i].ToString(), FileMode.Open);      
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    entry.Size = fs.Length;
    fs.Close();
    crc.Reset();
    crc.Update(buffer);
    entry.Crc = crc.Value;
    output.PutNextEntry(entry);
    output.Write(buffer, 0, buffer.Length);
}

output.Finish();
output.Close();
response.Clear();
response.ContentType = "D:/Work Area/";
response.AddHeader("Content-Disposition", "attachment; filename=" + "download.zip");
response.End();

有人能告诉我问题出在哪吗?为什么这些文件不能以 .zip 的格式提供?


也许这个例子可以帮到你:http://www.codeproject.com/Articles/23010/Compress-Folders-with-C-and-the-SharpZipLib - Rohit Vipin Mathews
2个回答

1

在浏览SharpDevelop 维基之后,我找到了下面的内容,可能对你解决问题有所帮助:

using ICSharpCode.SharpZipLib.Zip;

// This will accumulate each of the files named in the fileList into a zip file,
// and stream it to the browser.
// This approach writes directly to the Response OutputStream.
// The browser starts to receive data immediately which should avoid timeout problems.
// This also avoids an intermediate memorystream, saving memory on large files.
//
private void DownloadZipToBrowser(List <string> zipFileList) 
{
    Response.ContentType = "application/zip";
    // If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that
    //    Response.ContentType = "application/octet-stream"     has solved this. May be specific to Internet Explorer.

    Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\"");
    response.CacheControl = "Private";
    response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); // or put a timestamp in the filename in the content-disposition

    byte[] buffer = new byte[4096];

    ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
    zipOutputStream.SetLevel(3); //0-9, 9 being the highest level of compression

    foreach (string fileName in zipFileList) {

        Stream fs = File.OpenRead(fileName);    // or any suitable inputstream

        ZipEntry entry = new ZipEntry(ZipEntry.CleanName(fileName));
        entry.Size = fs.Length;
        // Setting the Size provides WinXP built-in extractor compatibility,
        //  but if not available, you can set zipOutputStream.UseZip64 = UseZip64.Off instead.

        zipOutputStream.PutNextEntry(entry);

        int count = fs.Read(buffer, 0, buffer.Length);
        while (count > 0) {
            zipOutputStream.Write(buffer, 0, count);
            count = fs.Read(buffer, 0, buffer.Length);
            if (!Response.IsClientConnected) {
                break;
            }
            Response.Flush();
        }
        fs.Close();
    }
    zipOutputStream.Close();

    Response.Flush();
    Response.End(); 
}

这应该可以工作,但你只应该用于小文件,因为最终压缩包的大小不可用,所以客户端浏览器无法显示进度。如果文件更大,请在内存中创建zip(MemoryStream),然后将其大小和内容写入响应。 - Christoph Fink

1

CRC32对应于每个压缩文件。

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

//stream directly to client.
ICSharpCode.SharpZipLib.Zip.ZipOutputStream output = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(response.OutputStream);
output.SetLevel(9);
string[] files =    Directory.GetFiles("D:/newfolder/");
for (int i = 0; i < files.Length; i++)
{
    ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
    ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(files[i].ToString());
    entry.DateTime = DateTime.Now;
    System.IO.FileStream fs = new System.IO.FileStream(files[i].ToString(), FileMode.Open);      
    byte[] buffer = new byte[fs.Length];
    fs.Read(buffer, 0, buffer.Length);
    entry.Size = fs.Length;
    fs.Close();
    crc.Reset();
    crc.Update(buffer);
    entry.Crc = crc.Value;
    output.PutNextEntry(entry);
    output.Write(buffer, 0, buffer.Length);
}

output.Finish();
output.Close();
response.Clear();
response.ContentType = "D:/Work Area/";
response.AddHeader("Content-Disposition", "attachment; filename=" + "download.zip");
response.End();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接