ICSharpCode.SharpZLib的使用记录
2012-05-03 18:52
453 查看
====同事指点的==================
private void StartZip(string fileName, string sourceFolderPath, string targetPath)
{
try
{
// linkLabel1.Text = "";
string tempDirectory = System.IO.Path.GetTempPath() + "\\";
string tempFilename = "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileName.Trim();
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(tempDirectory + tempFilename)))
{
// store only
zipStream.SetLevel(0);
// Disable Zip64 for Mac compatibility
zipStream.UseZip64 = UseZip64.Off;
string path = sourceFolderPath;
// This setting will strip the leading part of the folder path in the entries, to
// make the entries relative to the starting folder.
// To include the full path for each entry up to the drive root, assign folderOffset = 0.
int folderOffset = path.Length + (path.EndsWith("\\") ? 0 : 1);
AddZipEntry(path, zipStream, folderOffset);
zipStream.Finish();
zipStream.Close();
}
FileInfo zipFile = new FileInfo(tempDirectory + tempFilename);
if (zipFile.Exists)
{
string destDirectory = targetPath;
if (!destDirectory.EndsWith("\\"))
{
destDirectory += "\\";
}
string destFile = fileName;
_zipDestPath = destDirectory + destFile;
zipFile.CopyTo(_zipDestPath);
//linkLabel1.Text = "Completed. Click here to show zip file.";
//linkLabel1.Click += new EventHandler(linkLabel1_Click);
}
}
catch (Exception ex)
{
//linkLabel1.Text = "Errors encountered. Cannot continue. " + ex.InnerException;
}
}
private void AddZipEntry(string path, ZipOutputStream zipStream, int folderOffset)
{
string[] files = Directory.GetFiles(path);
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
newEntry.Size = fi.Length;
newEntry.CompressionMethod = CompressionMethod.Stored;
zipStream.PutNextEntry(newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
AddZipEntry(folder, zipStream, folderOffset);
}
}
====以下是转的==================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Core;
namespace TestConsole
{
class Program
{
static string path = @"H:\New folder\folder\";
static void Main()
{
CreateZipFile(@"H:\New folder\folder", @"H:\New folder\folder.zip");
//UnZipFile(@"d:\a.zip");
}
private static void CreateZipFile(string filesPath, string zipFilePath)
{
ZipOutputStream u = new ZipOutputStream(File.Create(zipFilePath)); //新建压缩文件流 “ZipOutputStream”
AddZipEntry(filesPath, u, out u); //向压缩文件流加入内容
u.Finish(); // 结束压缩
u.Close();
}
//添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream; out j为已添加“ZipEntry”的“ZipOutputStream”
public static void AddZipEntry(string p, ZipOutputStream u, out ZipOutputStream j)
{
string s = p;
if (Directory.Exists(s)) //文件夹的处理
{
DirectoryInfo di = new DirectoryInfo(s);
var tempd = di.GetDirectories();
//***********以下内容是修订后添加的***********
if (tempd.Length <= 0) //没有子目录
{
ZipEntry z = new ZipEntry(ShortDir(p) + "//"); //末尾“//”用于文件夹的标记
//u.PutNextEntry(z);
}
//***************以上内容是修订后添加的***************
foreach (DirectoryInfo tem in tempd) //获取子目录
{
ZipEntry z = new ZipEntry(ShortDir(tem.FullName)); //末尾“//”用于文件夹的标记
//u.PutNextEntry(z); //此句不可少,否则空目录不会被添加
s = tem.FullName;
AddZipEntry(s, u, out u); //递归
}
var t = di.GetFiles();
foreach (FileInfo temp in t) //获取此目录的文件
{
s = temp.FullName;
AddZipEntry(s, u, out u); //递归
}
}
else if (File.Exists(s)) //文件的处理
{
u.SetLevel(0); //压缩等级
FileStream f = File.OpenRead(s);
byte[] b = new byte[f.Length];
f.Read(b, 0, b.Length); //将文件流加入缓冲字节中
ZipEntry z = new ZipEntry(ShortDir(s));
u.PutNextEntry(z); //为压缩文件流提供一个容器
u.Write(b, 0, b.Length); //写入字节
f.Close();
}
j = u; //返回已添加数据的“ZipOutputStream”
}
private static string ShortDir(string s)
{
//将文件的绝对路径转为相对路径
string d = s.Replace(path, "");
return d;
}
}
}
========================这个最简单============================
private void StartZip(string fileName, string sourceFolderPath, string targetPath)
{
try
{
// linkLabel1.Text = "";
string tempDirectory = System.IO.Path.GetTempPath() + "\\";
string tempFilename = "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileName.Trim();
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(tempDirectory + tempFilename)))
{
// store only
zipStream.SetLevel(0);
// Disable Zip64 for Mac compatibility
zipStream.UseZip64 = UseZip64.Off;
string path = sourceFolderPath;
// This setting will strip the leading part of the folder path in the entries, to
// make the entries relative to the starting folder.
// To include the full path for each entry up to the drive root, assign folderOffset = 0.
int folderOffset = path.Length + (path.EndsWith("\\") ? 0 : 1);
AddZipEntry(path, zipStream, folderOffset);
zipStream.Finish();
zipStream.Close();
}
FileInfo zipFile = new FileInfo(tempDirectory + tempFilename);
if (zipFile.Exists)
{
string destDirectory = targetPath;
if (!destDirectory.EndsWith("\\"))
{
destDirectory += "\\";
}
string destFile = fileName;
_zipDestPath = destDirectory + destFile;
zipFile.CopyTo(_zipDestPath);
//linkLabel1.Text = "Completed. Click here to show zip file.";
//linkLabel1.Click += new EventHandler(linkLabel1_Click);
}
}
catch (Exception ex)
{
//linkLabel1.Text = "Errors encountered. Cannot continue. " + ex.InnerException;
}
}
private void AddZipEntry(string path, ZipOutputStream zipStream, int folderOffset)
{
string[] files = Directory.GetFiles(path);
foreach (string filename in files)
{
FileInfo fi = new FileInfo(filename);
string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder
entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
newEntry.Size = fi.Length;
newEntry.CompressionMethod = CompressionMethod.Stored;
zipStream.PutNextEntry(newEntry);
// Zip the file in buffered chunks
// the "using" will close the stream even if an exception occurs
byte[] buffer = new byte[4096];
using (FileStream streamReader = File.OpenRead(filename))
{
StreamUtils.Copy(streamReader, zipStream, buffer);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
AddZipEntry(folder, zipStream, folderOffset);
}
}
====以下是转的==================
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;
using ICSharpCode.SharpZipLib.Core;
namespace TestConsole
{
class Program
{
static string path = @"H:\New folder\folder\";
static void Main()
{
CreateZipFile(@"H:\New folder\folder", @"H:\New folder\folder.zip");
//UnZipFile(@"d:\a.zip");
}
private static void CreateZipFile(string filesPath, string zipFilePath)
{
ZipOutputStream u = new ZipOutputStream(File.Create(zipFilePath)); //新建压缩文件流 “ZipOutputStream”
AddZipEntry(filesPath, u, out u); //向压缩文件流加入内容
u.Finish(); // 结束压缩
u.Close();
}
//添加压缩项目:p 为需压缩的文件或文件夹; u 为现有的源ZipOutputStream; out j为已添加“ZipEntry”的“ZipOutputStream”
public static void AddZipEntry(string p, ZipOutputStream u, out ZipOutputStream j)
{
string s = p;
if (Directory.Exists(s)) //文件夹的处理
{
DirectoryInfo di = new DirectoryInfo(s);
var tempd = di.GetDirectories();
//***********以下内容是修订后添加的***********
if (tempd.Length <= 0) //没有子目录
{
ZipEntry z = new ZipEntry(ShortDir(p) + "//"); //末尾“//”用于文件夹的标记
//u.PutNextEntry(z);
}
//***************以上内容是修订后添加的***************
foreach (DirectoryInfo tem in tempd) //获取子目录
{
ZipEntry z = new ZipEntry(ShortDir(tem.FullName)); //末尾“//”用于文件夹的标记
//u.PutNextEntry(z); //此句不可少,否则空目录不会被添加
s = tem.FullName;
AddZipEntry(s, u, out u); //递归
}
var t = di.GetFiles();
foreach (FileInfo temp in t) //获取此目录的文件
{
s = temp.FullName;
AddZipEntry(s, u, out u); //递归
}
}
else if (File.Exists(s)) //文件的处理
{
u.SetLevel(0); //压缩等级
FileStream f = File.OpenRead(s);
byte[] b = new byte[f.Length];
f.Read(b, 0, b.Length); //将文件流加入缓冲字节中
ZipEntry z = new ZipEntry(ShortDir(s));
u.PutNextEntry(z); //为压缩文件流提供一个容器
u.Write(b, 0, b.Length); //写入字节
f.Close();
}
j = u; //返回已添加数据的“ZipOutputStream”
}
private static string ShortDir(string s)
{
//将文件的绝对路径转为相对路径
string d = s.Replace(path, "");
return d;
}
}
}
========================这个最简单============================
解压缩代码: using System; using System.Collections.Generic; using System.Text; using ICSharpCode.SharpZipLib.Zip; using System.IO; namespace DRMEncryption { /// ;summary; /// UnZip 类用于解压缩一个 zip 文件。 /// ;/summary; public class UnZipDir { //解压缩以后的文件名和路径,压缩前的路径 public static Boolean UNZipFile(string FileToZip, string ZipedFile) { try { FastZip fastZip = new FastZip(); fastZip.ExtractZip(FileToZip, ZipedFile, ""); return true; } catch { return false; } } } } 压缩文件代码: using System; using System.IO; using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip; namespace DRMEncryption { public class ZipClass { public static Boolean ZipFile(string FileToZip, string ZipedFile) { try { FastZip fastZip = new FastZip(); bool recurse = true; //压缩后的文件名,压缩目录 ,是否递归 fastZip.CreateZip(FileToZip, ZipedFile, recurse, ""); return true; } catch { return false; } } } } 那个返回方法是用来另外一个页面判断解压是否成功用的,可以不要; 解压缩:UnZipDir.UNZipFile(文件名, 路径); 压缩:ZipClass.ZipFile(文件名, 路径);
相关文章推荐
- ICSharpCode.SharpZipLib 使用演示
- ICSharpCode.SharpZipLib 开源压缩库使用示例
- ICSharpCode.SharpZipLib.dll 使用方法
- ICSharpCode.TextEditor使用及扩展
- ICSharpCode.SharpZipLib.Zip使用示例代码及下载地址
- ICSharpCode.SharpZipLib 初级使用
- ICSharpCode.SharpZipLib .dll 使用说明
- ICSharpCode.TextEditor使用及扩展
- 开源Zip文件压缩算法 ICSharpCode.SharpZLib
- [转]ICSharpCode.SharpZipLib 初级使用
- ICSharpCode.SharpZipLib.dll,MyZip.dll,Ionic.Zip.dll 使用
- 使用 ICSharpCode.SharpZipLib.dll 压缩文件 方法实例
- ICSharpCode.SharpZipLib.Zip 使用
- ICSharpCode.SharpZipLib
- 在ASP.NET MVC中使用Log4Net进行多种HttpCode日志的记录
- 原来设置ICSharpCode.TextEditor语法高亮这么简单
- ICSharpCode.TextEditor扩展——增加右键菜单
- 未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf116
- 压缩/解压(ICSharpCode.SharpZipLib 类库)
- 网络传输压缩DLL: ICSharpCode.SharpZipLib