我一直在玩C#的SharpZip Library(版本0.86.0)。我基本上用它将一些文件打包成一个干净的zip文件。这是我的函数看起来要生成zip文件的字节数组:
public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3)
{
MemoryStream zipMemoryStream = new MemoryStream();
ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream);
zOutput.SetLevel(zipLevel);
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
foreach (var file in fileItemList)
{
ZipEntry entry = new ZipEntry(file.Item2);
entry.DateTime = DateTime.Now;
entry.Size = file.Item1.Length;
crc.Reset();
crc.Update(file.Item1);
entry.Crc = crc.Value;
zOutput.PutNextEntry(entry);
zOutput.Write(file.Item1, 0, file.Item1.Length);
}
zOutput.IsStreamOwner = false;
zOutput.Finish();
zOutput.Close();
zipMemoryStream.Position = 0;
byte[] zipedFile = zipMemoryStream.ToArray();
return zipedFile;
}
该函数适用于包含一个项目的文件。但出于某些原因,当我有两个或更多时,我会在提取/打开它时遇到错误。
PeaZip说:
存档不可读
WinZip说:
存储在此文件的本地标头中的压缩大小与存储在中央标头中的压缩大小不同
但这是踢球者。 Windows 8存档工具可以正常使用该文件。 WinZip错误让我觉得我正在错误地将文件写入流中。但它对我来说很好看。不知道该怎么做..
修改
这是我对codemonkeys输入的更改。对我来说看起来更好,但我仍然遇到同样的错误
public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3)
{
MemoryStream zipMemoryStream = new MemoryStream();
ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream);
zOutput.SetLevel(zipLevel);
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
foreach (var file in fileItemList)
{
ZipEntry entry = new ZipEntry(file.Item2);
entry.DateTime = DateTime.Now;
entry.Size = file.Item1.Length;
crc.Reset();
crc.Update(file.Item1);
entry.Crc = crc.Value;
zOutput.PutNextEntry(entry);
var memStreamCurrentfile = new MemoryStream(file.Item1);
StreamUtils.Copy(memStreamCurrentfile, zOutput, new byte[4096]);
zOutput.CloseEntry();
}
zOutput.IsStreamOwner = false;
zOutput.Finish();
zOutput.Close();
zipMemoryStream.Position = 0;
byte[] zipedFile = zipMemoryStream.ToArray();
return zipedFile;
}
答案 0 :(得分:0)
想出来!看来我设置Crc和文件条目的大小是个问题。我认为这将有助于定义这些。我想我错了。这是所有人享受的最终代码:
public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3)
{
MemoryStream zipMemoryStream = new MemoryStream();
ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream);
zOutput.SetLevel(zipLevel);
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
foreach (var file in fileItemList)
{
ZipEntry entry = new ZipEntry(file.Item2);
entry.DateTime = DateTime.Now;
zOutput.PutNextEntry(entry);
var memStreamCurrentfile = new MemoryStream(file.Item1);
StreamUtils.Copy(memStreamCurrentfile, zOutput, new byte[4096]);
zOutput.CloseEntry();
}
zOutput.IsStreamOwner = false;
zOutput.Finish();
zOutput.Close();
zipMemoryStream.Position = 0;
byte[] zipedFile = zipMemoryStream.ToArray();
return zipedFile;
}