单文件压缩格式,表示加压后为你单独压缩的文件
=======================================================================
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="FileToZip">要进行压缩的文件名</param>
/// <param name="ZipedFile">压缩后生成的压缩文件名</param>
/// <returns></returns>
public bool ZipFile(string FileToZip, string ZipedFile)
{
//如果文件没有找到,则报错
if (!File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("指定要压缩的文件: " + FileToZip + " 不存在!");
}
//FileStream fs = null;
FileStream ZipFile = null;
ZipOutputStream ZipStream = null;
ZipEntry ZipEntry = null;
bool res = true;
try
{
ZipFile = File.OpenRead(FileToZip);
byte[] buffer = new byte[ZipFile.Length];
ZipFile.Read(buffer, 0, buffer.Length);
ZipFile.Close();
ZipFile = File.Create(ZipedFile);
ZipStream = new ZipOutputStream(ZipFile);
ZipEntry = new ZipEntry(Path.GetFileName(FileToZip));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
ZipStream.Write(buffer, 0, buffer.Length);
}
catch
{
res = false;
}
finally
{
if (ZipEntry != null)
{
ZipEntry = null;
}
if (ZipStream != null)
{
ZipStream.Finish();
ZipStream.Close();
}
if (ZipFile != null)
{
ZipFile.Close();
ZipFile = null;
}
GC.Collect();
GC.Collect(1);
}
return res;
}
---------------------------------------------------
用法:
ZipClass.ZipClass Zc = new ZipClass.ZipClass();
//ZipClass是一个名为ZipClass类库中的ZipClass类 ,
//实例一个类的对象就可以调用里面的方法了
Zc.ZipFile("D:\\下载程序\\WebDownloadService\\WebDownloadService\\UploadFile\\sjpt.txt", "D:\\下载程序\\WebDownloadService\\WebDownloadService\\UploadFile\\sjpt.zip");
=====================================================
全路径压缩格式,指的是 你解压后的文件 是一个带有压缩路径的文件,而你需要的文件在层层路径的文件夹里
---------
//public void ZipFileMain(string[] args)
// {
// string[] filenames = Directory.GetFiles(args[0]);
// Crc32 crc = new Crc32();
// ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));
// s.SetLevel(6); // 0 - store only to 9 - means best compression
// foreach (string file in filenames)
// {
// //打开压缩文件
// FileStream fs = File.OpenRead(file);
// byte[] buffer = new byte[fs.Length];
// fs.Read(buffer, 0, buffer.Length);
// ZipEntry entry = new ZipEntry(file);
// entry.DateTime = DateTime.Now;
// // set Size and the crc, because the information
// // about the size and crc should be stored in the header
// // if it is not set it is automatically written in the footer.
// // (in this case size == crc == -1 in the header)
// // Some ZIP programs have problems with zip files that don't store
// // the size and crc in the header.
// entry.Size = fs.Length;
// fs.Close();
// crc.Reset();
// crc.Update(buffer);
// entry.Crc = crc.Value;
// s.PutNextEntry(entry);
// s.Write(buffer, 0, buffer.Length);
// }
// s.Finish();
// s.Close();
// }
----------------------------------------------------
用法:
//string[] FileProperties = new string[2];
//FileProperties[0] = @"D:\下载程序\WebDownloadService\WebDownloadService\UploadFile";//待压缩文件目录
//FileProperties[1] = @"D:\下载程序\WebDownloadService\WebDownloadService\UploadFile\sjpt.zip"; //压缩后的目标文件
//ZipClass.ZipClass Zc = new ZipClass.ZipClass();
//Zc.ZipFileMain(FileProperties);
//Response.Write("文件已经压缩成功!");