我的代码中有以下方法:
private bool GenerateZipFile(ListfilesToArchive, 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
代码实际上取自样本,但我似乎缺少一些东西.有没有人有任何指针?
我曾经使用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); }