SharpZipLib压缩 和 WINRAR选择ZIP模式压缩 有什么不同?大小好象有点不一样。我主要压缩出来是改成JAR使用。JAR就是使用ZIP压缩方法,以前我都是用WINRAR选择ZIP模式压缩,我这次我需要批量处理一些东西,结果就出问题了。我用的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;
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;
不过我的是xml文件,还好能正常用,别的文件类型就不清楚了