ICSharpCode.SharpZipLib.dll使用指南
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using System.Diagnostics;
namespace FileOperations {
public class SharpZip {
public SharpZip() {}
///
/// 压缩文件夹
///
/// 压缩后的文件名(包含物理路径)
/// 待压缩的文件夹(包含物理路径)
public static void PackFiles(string filename, string directory) {
try {
FastZip fz = new FastZip {
CreateEmptyDirectories = true
};
fz.CreateZip(filename, directory, true, "");
} catch (Exception) {
throw;
}
}
///
/// 解压文件
///
/// 待解压文件名(包含物理路径)
/// 解压到的目录(包含物理路径)
public static bool UnpackFiles(string file, string dir) {
try {
if (!Directory.Exists(dir)) {
Directory.CreateDirectory(dir);
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(file))) {
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (!string.IsNullOrEmpty(directoryName)) {
Directory.CreateDirectory(Path.Combine(dir, directoryName));
}
if (!string.IsNullOrEmpty(fileName)) {
using (FileStream streamWriter = File.Create(Path.Combine(dir, theEntry.Name))) {
int size = 2048;
byte[] data = new byte[size];
while ((size = s.Read(data, 0, data.Length)) > 0) {
streamWriter.Write(data, 0, size);
}
}
}
}
}
return true;
} catch (Exception) {
throw;
}
}
}
public class ClassZip {
private static bool ZipFileDirectory(string folderToZip, ZipOutputStream s, string parentFolderName) {
bool res = true;
try {
string[] filenames = Directory.GetFiles(folderToZip);
s.PutNextEntry(new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/")));
s.Flush();
foreach (string file in filenames) {
using (FileStream fs = File.OpenRead(file)) {
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(Path.Combine(parentFolderName, Path.GetFileName(folderToZip) + "/" + Path.GetFileName(file))) {
DateTime = DateTime.Now,
Size = fs.Length
};
Crc32 crc = new Crc32();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
} catch {
res = false;
}
string[] folders = Directory.GetDirectories(folderToZip);
foreach (string folder in folders) {
if (!ZipFileDirectory(folder, s, Path.Combine(parentFolderName, Path.GetFileName(folderToZip)))) {
return false;
}
}
return res;
}
public static bool Zip(string fileToZip, string zipedFile, int level) {
if (Directory.Exists(fileToZip)) {
return ZipFileDirectory(fileToZip, new ZipOutputStream(File.Create(zipedFile)) { SetLevel = level }, "");
} else if (File.Exists(fileToZip)) {
using (FileStream zipFile = File.Create(zipedFile))
using (ZipOutputStream zipStream = new ZipOutputStream(zipFile) { SetLevel = level })
using (FileStream fs = File.OpenRead(fileToZip)) {
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(Path.GetFileName(fileToZip));
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
return true;
}
return false;
}
public static void UnZip(string fileToUpZip, string zipedFolder) {
if (!File.Exists(fileToUpZip) || !Directory.Exists(zipedFolder)) {
Directory.CreateDirectory(zipedFolder);
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(fileToUpZip))) {
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
if (!string.IsNullOrEmpty(theEntry.Name)) {
string fileName = Path.Combine(zipedFolder, theEntry.Name);
if (fileName.EndsWith("/") || fileName.EndsWith("")) {
Directory.CreateDirectory(fileName);
} else {
using (FileStream streamWriter = File.Create(fileName)) {
int size = 2048;
byte[] data = new byte[size];
while ((size = s.Read(data, 0, data.Length)) > 0) {
streamWriter.Write(data, 0, size);
}
}
}
}
}
}
}
}
public class ZipHelper {
private string the_rar;
private RegistryKey the_Reg;
private object the_Obj;
private string the_Info;
private ProcessStartInfo the_StartInfo;
private Process the_Process;
public void EnZip(string zipname, string zippath, string dirpath) {
try {
the_Reg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRAR.exeShellOpenCommand");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString().Substring(1, the_Obj.ToString().Length - 7);
the_Reg.Close();
the_Info = " a " + zipname + " " + zippath;
the_StartInfo = new ProcessStartInfo {
FileName = the_rar,
Arguments = the_Info,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = dirpath
};
the_Process = new Process {
StartInfo = the_StartInfo
};
the_Process.Start();
} catch (Exception ex) {
throw new Exception(ex.Message);
}
}
public void DeZip(string zipname, string zippath) {
try {
the_Reg = Registry.ClassesRoot.OpenSubKey(@"ApplicationsWinRAR.exeShellOpenCommand");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString().Substring(1, the_Obj.ToString().Length - 7);
the_Reg.Close();
the_Info = " X " + zipname + " " + zippath;
the_StartInfo = new ProcessStartInfo {
FileName = the_rar,
Arguments = the_Info,
WindowStyle = ProcessWindowStyle.Hidden
};
the_Process = new Process {
StartInfo = the_StartInfo
};
the_Process.Start();
} catch (Exception ex) {
throw new Exception(ex.Message);
}
}
}
}
240.25KB
文件大小:
评论区