SharpZipLib 的基础知识。 我缺少什么?

发布于 2024-07-09 13:28:51 字数 1497 浏览 11 评论 0原文

我的代码中有以下方法:

private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
{
    try
    {
        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(GetZipFileName(archiveDate))))
        {
            zipStream.SetLevel(9); // maximum compression.
            byte[] buffer = new byte[4096];

            foreach (FileInfo fi in filesToArchive)
            {
                string fileName = ZipEntry.CleanName(fi.Name);
                ZipEntry entry = new ZipEntry(fileName);
                entry.DateTime = fi.LastWriteTime;
                zipStream.PutNextEntry(entry);

                using (FileStream fs = File.OpenRead(fi.FullName))
                {
                    StreamUtils.Copy(fs, zipStream, buffer);
                }

                zipStream.CloseEntry();
            }

            zipStream.Finish();
            zipStream.Close();
        }
        return true; 
    }
    catch (Exception ex)
    {
        OutputMessage(ex.ToString());
        return false;
    }
}

此代码生成一个包含所有正确条目的 ZIP 文件,但每个文件被列为 4 TB(未打包和打包),并且当我尝试打开它时会产生以下错误:

Extracting to "C:\winnt\profiles\jbladt\LOCALS~1\Temp\"
Use Path: no   Overlay Files: yes
skipping: QPS_Inbound-20081113.txt: this file is not in the standard Zip 2.0 format
   Please see www.winzip.com/zip20.htm for more information
error:  no files were found - nothing to do

代码实际上是从样本中获取的,但我似乎遗漏了一些东西。 有人有任何指点吗?

I have the following method in my code:

private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
{
    try
    {
        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(GetZipFileName(archiveDate))))
        {
            zipStream.SetLevel(9); // maximum compression.
            byte[] buffer = new byte[4096];

            foreach (FileInfo fi in filesToArchive)
            {
                string fileName = ZipEntry.CleanName(fi.Name);
                ZipEntry entry = new ZipEntry(fileName);
                entry.DateTime = fi.LastWriteTime;
                zipStream.PutNextEntry(entry);

                using (FileStream fs = File.OpenRead(fi.FullName))
                {
                    StreamUtils.Copy(fs, zipStream, buffer);
                }

                zipStream.CloseEntry();
            }

            zipStream.Finish();
            zipStream.Close();
        }
        return true; 
    }
    catch (Exception ex)
    {
        OutputMessage(ex.ToString());
        return false;
    }
}

This code generates a ZIP file with all the correct entries, but each file is listed as being 4 TB (both unpacked and packed) and creates the following error when I try to open it:

Extracting to "C:\winnt\profiles\jbladt\LOCALS~1\Temp\"
Use Path: no   Overlay Files: yes
skipping: QPS_Inbound-20081113.txt: this file is not in the standard Zip 2.0 format
   Please see www.winzip.com/zip20.htm for more information
error:  no files were found - nothing to do

The code is practically taken from the samples, but I seem to be missing something. Does anyone have any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和我恋爱吧 2024-07-16 13:28:51

我曾经使用 SharpZipLib,直到我切换到 DotNetZip 您可能想将其作为替代方案。

例子:

try
   {
     using (ZipFile zip = new ZipFile("MyZipFile.zip")
     {
       zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
       zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
       zip.AddFile("ReadMe.txt");
       zip.Save();
     }
   }
   catch (System.Exception ex1)
   {
     System.Console.Error.WriteLine("exception: " + ex1);
   }

I used to use SharpZipLib until I switched to DotNetZip You may want to check it out as an alternative.

Example:

try
   {
     using (ZipFile zip = new ZipFile("MyZipFile.zip")
     {
       zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
       zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf");
       zip.AddFile("ReadMe.txt");
       zip.Save();
     }
   }
   catch (System.Exception ex1)
   {
     System.Console.Error.WriteLine("exception: " + ex1);
   }
从﹋此江山别 2024-07-16 13:28:51

帖子

请参阅 Tyler Holmes的 Winzip 8.0 和其他版本都是 Zip64。 添加 ZipEntry 时设置原始文件大小,错误就会消失。

例如,

string fileName = ZipEntry.CleanName(fi.Name);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = fi.LastWriteTime;
entry.Size = fi.Length;
zipStream.PutNextEntry(entry);

当前版本的 zip 实用程序没有这个问题。

See the post by Tyler Holmes

The issue with Winzip 8.0 and others is with Zip64. Set the original file size when adding the ZipEntry and the error goes away.

e.g.

string fileName = ZipEntry.CleanName(fi.Name);
ZipEntry entry = new ZipEntry(fileName);
entry.DateTime = fi.LastWriteTime;
entry.Size = fi.Length;
zipStream.PutNextEntry(entry);

Current release zip utilities don't have the problem.

听风念你 2024-07-16 13:28:51

我遇到了类似的问题,通过在 ZipEntry 对象上指定 CompressionMethod 和 CompressedSize 属性解决了这个问题。 然而,在我的使用中,创建 zip 是为了将几个非常小的文件分组到一次下载中,而不是实际压缩文件,因此我没有使用任何压缩(级别 0)并使用文件的实际大小作为 CompressedSize 属性。 不知道如果需要压缩,这将如何工作。

I had a similar problem which I solved by specifying the CompressionMethod and CompressedSize properties on the ZipEntry object. In my usage, however, the zip was being created to group several very small files in one download rather than actually compressing the files, so I got away with using no compression (level 0) and using the file's actual size for the CompressedSize property. Not sure how that would work if compression is necessary.

美胚控场 2024-07-16 13:28:51

为了将来遇到同样问题的人的利益:我的问题原来是我使用的是真正古老的 WinZip 版本(我认为是 8.0)来查看文件。 使用现代查看器(12.0)解决了这个问题。

For the benefit of anyone having the same problem in the future: My problem turned out to be that I was using a truly ancient version of WinZip (8.0, I think) to view the files. Using a modern viewer (12.0) solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文