是否有用于压缩大量文件的库或工具

发布于 2024-08-17 15:39:09 字数 1689 浏览 6 评论 0原文

我已经下载了 ICSharpCode.SharpZipLib 和 DotNetZip。我一次压缩 100 多个文件,大小从 1 兆到 4 兆不等。当我使用 ICSharpCode 时,出现“ContextSwitchDeadlock”错误。 DotNetZip 每次完成文件时都会失败。

另外,我正在处理共享点文件夹(映射到我的本地驱动器)

private bool zipall()
//ICSharpCode
{
    int i = 0;
    progressBarzipping.Minimum = 0;
    progressBarzipping.Maximum = listBoxfiles.Items.Count;
    ZipOutputStream zipOut = new ZipOutputStream(File.Create(textBoxDropPath.Text + "\\" + textBoxZipFileName.Text + ".zip"));
    foreach (string fName in listBoxfiles.Items)
    {
        try
        {
            FileInfo fi = new FileInfo(fName);
            ZipEntry entry = new ZipEntry(fi.Name);
            FileStream sReader = File.OpenRead(fName);
            byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
            sReader.Read(buff, 0, (int)sReader.Length);
            entry.DateTime = fi.LastWriteTime;
            entry.Size = sReader.Length;
            sReader.Close();
            zipOut.PutNextEntry(entry);
            zipOut.Write(buff, 0, buff.Length);
        }
        catch
        {
            MessageBox.Show("Zip Failed");
            zipOut.Finish();
            zipOut.Close();
            progressBarzipping.Value = 0;
            return false;
        }
        i++;
        progressBarzipping.Value = i;
    }
    zipOut.Finish();
    zipOut.Close();
    MessageBox.Show("Zip Complete");
    progressBarzipping.Value = 0;
    return true;

}

//Not sure but I think this was my DotNetZip approach
//using (ZipFile zip = new ZipFile())
//  {
//     foreach(string file in listboxFiles.Items)
//       {
//         zip.AddFile(file);
//       }      
//  zip.Save(PathToNewZip);
//  }

I've downloaded ICSharpCode.SharpZipLib and DotNetZip. I'm zipping over 100 files at a time varying a meg to 4 megs. When I use ICSharpCode I get a 'ContextSwitchDeadlock' error. DotNetZip fails on the finalizing of the file every time.

Also, I'm working on sharepoint folders (mapped to my local drive)

private bool zipall()
//ICSharpCode
{
    int i = 0;
    progressBarzipping.Minimum = 0;
    progressBarzipping.Maximum = listBoxfiles.Items.Count;
    ZipOutputStream zipOut = new ZipOutputStream(File.Create(textBoxDropPath.Text + "\\" + textBoxZipFileName.Text + ".zip"));
    foreach (string fName in listBoxfiles.Items)
    {
        try
        {
            FileInfo fi = new FileInfo(fName);
            ZipEntry entry = new ZipEntry(fi.Name);
            FileStream sReader = File.OpenRead(fName);
            byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
            sReader.Read(buff, 0, (int)sReader.Length);
            entry.DateTime = fi.LastWriteTime;
            entry.Size = sReader.Length;
            sReader.Close();
            zipOut.PutNextEntry(entry);
            zipOut.Write(buff, 0, buff.Length);
        }
        catch
        {
            MessageBox.Show("Zip Failed");
            zipOut.Finish();
            zipOut.Close();
            progressBarzipping.Value = 0;
            return false;
        }
        i++;
        progressBarzipping.Value = i;
    }
    zipOut.Finish();
    zipOut.Close();
    MessageBox.Show("Zip Complete");
    progressBarzipping.Value = 0;
    return true;

}

//Not sure but I think this was my DotNetZip approach
//using (ZipFile zip = new ZipFile())
//  {
//     foreach(string file in listboxFiles.Items)
//       {
//         zip.AddFile(file);
//       }      
//  zip.Save(PathToNewZip);
//  }

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

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

发布评论

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

评论(1

梦纸 2024-08-24 15:39:09

你没有提供例外。使用 DotNetZip 时,我猜想问题可能出在共享点映射驱动器上。 DotNetZip 通常会将 zip 保存为临时文件,然后重命名。也许由于共享点,这不起作用。如果是这种情况,请尝试打开文件流并将其保存到该流。这避免了重命名操作。

progressBarzipping.Minimum = 0; 
progressBarzipping.Maximum = listBoxfiles.Items.Count;
using (Stream fs = new FileStream(PathToNewZip, FileMode.Create, FileAccess.Write))
{
    using (ZipFile zip = new ZipFile()) 
    { 
       zip.AddFiles(listboxFiles.Items);

       // do the progress bar: 
       zip.SaveProgress += (sender, e) => {
          if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry) {
             progressBarzipping.PerformStep();
          }
       };

       zip.Save(fs); 
    }
} 

You didn't provide the exception. When using DotNetZip, I guess the problem might be with the sharepoint mapped drive. DotNetZip normally will save the zip as a temp file, then rename it. Maybe this isn't working because of sharepoint. If that's the case, try opening a filestream and saving it to that stream. This avoids the rename operation.

progressBarzipping.Minimum = 0; 
progressBarzipping.Maximum = listBoxfiles.Items.Count;
using (Stream fs = new FileStream(PathToNewZip, FileMode.Create, FileAccess.Write))
{
    using (ZipFile zip = new ZipFile()) 
    { 
       zip.AddFiles(listboxFiles.Items);

       // do the progress bar: 
       zip.SaveProgress += (sender, e) => {
          if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry) {
             progressBarzipping.PerformStep();
          }
       };

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