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

时间:2010-01-11 18:14:32

标签: c# compression zip

我已经下载了ICSharpCode.SharpZipLib和DotNetZip。我正在翻译超过100个文件,一次变化为1兆到4兆。当我使用ICSharpCode时,我收到'ContextSwitchDeadlock'错误。每次最终确定文件时,DotNetZip都会失败。

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

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);
//  }

1 个答案:

答案 0 :(得分:0)

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

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); 
    }
}