C# 是否有用于压缩大量文件的库或工具
我已经下载了ICSharpCode.SharpZipLib和DotNetZip。我一次压缩100多个文件,从1兆到4兆不等。当我使用ICSHARP代码时,我得到一个“ContextSwitchDeadlock”错误。DotNetZip每次完成文件时都会失败 此外,我正在处理映射到本地驱动器的sharepoint文件夹C# 是否有用于压缩大量文件的库或工具,c#,compression,zip,C#,Compression,Zip,我已经下载了ICSharpCode.SharpZipLib和DotNetZip。我一次压缩100多个文件,从1兆到4兆不等。当我使用ICSHARP代码时,我得到一个“ContextSwitchDeadlock”错误。DotNetZip每次完成文件时都会失败 此外,我正在处理映射到本地驱动器的sharepoint文件夹 private bool zipall() //ICSharpCode { int i = 0; progressBarzipping.Minimum = 0;
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);
// }
你没有提供例外情况。使用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);
}
}
显示DotNetZip的代码和异常,我可以帮助您。它可以处理100000个或更多文件,10gb或更多。