SharpZipLib压缩代码如下:
using System;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
namespace Nbgzc
{
class ZipLib
{
Crc32 crc;
ZipOutputStream s;
string ZipDirectory;
public void ZipToFile(string _ZipDirectory, string _ZipFile)
{
if (!_ZipDirectory.EndsWith(“\”)) _ZipDirectory += “\”;
ZipDirectory = _ZipDirectory;
string[] filenames = Directory.GetFiles(_ZipDirectory);
string[] directorynames = Directory.GetDirectories(_ZipDirectory);
crc = new Crc32();
s = new ZipOutputStream(File.Create(_ZipFile));
s.SetLevel(5); // 0 – store only to 9 – means best compression
CycleDirectories(_ZipDirectory);
s.Finish();
s.Close();
}
private void CycleDirectories(string _ZipDirectory)
{
string[] filenames = Directory.GetFiles(_ZipDirectory);
string[] directorynames = Directory.GetDirectories(_ZipDirectory);
foreach (string file in filenames)
{
//MessageBox.Show(file);
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file.Replace(ZipDirectory, “”));
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
foreach (string directory in directorynames)
{
CycleDirectories(directory);
}
}
}
}
调用代码:
zl = new ZipLib();
zl.ZipToFile(“Cache/” + BookID.ToString() + “/Jar/”, “Cache/” + BookID.ToString() + “/” + BookID.ToString() + “.jar”);
zl = null;