110,526
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 使用zip方式压缩数据流
/// </summary>
/// <param name="filename">将数据流保存到zip包中使用的文件名</param>
/// <param name="filecontent">数据内容</param>
/// <returns>压缩后的zip数据</returns>
public static byte[] Zip(string filename, byte[] filecontent)
{
byte[] b = new byte[] { };
if (filecontent == null || filecontent.Length == 0)
return b;
//Crc32 crc = new Crc32();
MemoryStream ms = new MemoryStream();
ZipOutputStream zipOS = new ZipOutputStream(ms);
zipOS.SetLevel(6);
ZipEntry entry = new ZipEntry(filename);
entry.DateTime = DateTime.Now;
entry.Size = filecontent.Length;
//crc.Reset();
//crc.Update(filecontent);
//entry.Crc = crc.Value;
zipOS.PutNextEntry(entry);
zipOS.Write(filecontent, 0, filecontent.Length);
if (zipOS != null)
{
zipOS.Finish();
b = ms.ToArray();
zipOS.Close();
ms.Close();
}
return b;
}
假设你用SharpZip组件来做zip压缩。传入的是文件内容的byte[],以及在压缩包中显示的文件名,返回的是一个zip压缩包的byte[]