【问题标题】:how to zip all files with asp.net 3.5如何使用 asp.net 3.5 压缩所有文件
【发布时间】:2014-02-28 07:49:23
【问题描述】:

我是 .net 开发人员。我想用这种技术压缩所有文件并制作一个 zip 文件。

ZipFile multipleFilesAsZipFile = new ZipFile();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".zip");
                Response.ContentType = "application/zip";
                for (int i = 0; i < filename.Length; i++)
                {
                    string filePath = Server.MapPath("~/PostFiles/" + filename[i]);
                    multipleFilesAsZipFile.AddFile(filePath, string.Empty);
                }
multipleFilesAsZipFile.Save(Response.OutputStream);

我如何使用第三方库 Ionic 制作这个 Zip。

所有文件都已成功压缩,但未解压缩到客户端桌面。我的代码有问题吗?或者我正在使用的这个库已经过期。

是否有免费的完整版 .net 兼容库来压缩所有文件。

【问题讨论】:

  • 是的,我使用它...但几天后它仍然给我带来了问题,就像我在下面的问题中解释的那样,它给出了损坏的结果 zip 文件。

标签: asp.net-3.5


【解决方案1】:

使用 SharpZipLib:

Nuget 包

Install-Package SharpZipLib    

或在此处下载

http://www.icsharpcode.net/OpenSource/SharpZipLib/

示例摘录:

private void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset) {

    string[] files = Directory.GetFiles(path);

    foreach (string filename in files) {

        FileInfo fi = new FileInfo(filename);

        string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
        entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
        ZipEntry newEntry = new ZipEntry(entryName);
        newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity

        // Specifying the AESKeySize triggers AES encryption. Allowable values are 0 (off), 128 or 256.
                // A password on the ZipOutputStream is required if using AES.
        //   newEntry.AESKeySize = 256;

        // To permit the zip to be unpacked by built-in extractor in WinXP and Server2003, WinZip 8, Java, and other older code,
        // you need to do one of the following: Specify UseZip64.Off, or set the Size.
        // If the file may be bigger than 4GB, or you do not need WinXP built-in compatibility, you do not need either,
        // but the zip will be in Zip64 format which not all utilities can understand.
        //   zipStream.UseZip64 = UseZip64.Off;
        newEntry.Size = fi.Length;

        zipStream.PutNextEntry(newEntry);

        // Zip the file in buffered chunks
        // the "using" will close the stream even if an exception occurs
        byte[ ] buffer = new byte[4096];
        using (FileStream streamReader = File.OpenRead(filename)) {
            StreamUtils.Copy(streamReader, zipStream, buffer);
        }
        zipStream.CloseEntry();
    }
    string[ ] folders = Directory.GetDirectories(path);
    foreach (string folder in folders) {
        CompressFolder(folder, zipStream, folderOffset);
    }
}

取自:https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples

效果很棒!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多