利用ICSharpCode.SharpZipLib來實現的壓縮與解壓縮類
最近,在專案中經常需要處理壓縮和解壓縮檔案的操作。在網上找到了相關資料,自己整理了下,寫了兩個類:一個壓縮類;一個解壓縮類。
當然是利用了ICSharpCode.SharpZipLib提供的方法來對檔案壓縮和解壓縮。
壓縮類
///
/// 壓縮類
///
class ZipClass
{
private int compressionLevel = 9;
private byte[] buffer = new byte[2048];
///
/// 預設建構函式
///
public ZipClass()
{
}
///
/// 建構函式
///
/// 緩衝區大小
/// 壓縮率:0-9
public ZipClass(int bufferSize, int compressionLevel)
{
buffer = new byte[bufferSize];
this.compressionLevel = compressionLevel;
}
///
/// 壓縮檔案
///
/// 要壓縮的檔案路徑
/// 壓縮後的檔案路徑
public void ZipFile(string fileToZip, string zipedFile)
{
if (!File.Exists(fileToZip))
{
throw new FileNotFoundException("The specified file " + fileToZip + " could not be found.");
}
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedFile)))
{
string fileName = Path.GetFileName(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipStream.PutNextEntry(zipEntry);
zipStream.SetLevel(compressionLevel);
using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
{
int size = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, size);
while (size < streamToZip.Length)
{
int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
}
}
///
/// 得到檔案下的所有檔案
///
/// 資料夾路徑
///
public ArrayList GetFileList(string directory)
{
ArrayList fileList = new ArrayList();
bool isEmpty = true;
foreach (string file in Directory.GetFiles(directory))
{
fileList.Add(file);
isEmpty = false;
}
if (isEmpty)
{
if (Directory.GetDirectories(directory).Length == 0)
{
fileList.Add(directory + @"/");
}
}
foreach (string dirs in Directory.GetDirectories(directory))
{
foreach (object obj in GetFileList(dirs))
{
fileList.Add(obj);
}
}
return fileList;
}
///
/// 壓縮資料夾
///
/// 要壓縮的資料夾路徑
/// 壓縮或的資料夾路徑
public void ZipDerctory(string directoryToZip, string zipedDirectory)
{
using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipedDirectory)))
{
ArrayList fileList = GetFileList(directoryToZip);
int directoryNameLength = (Directory.GetParent(directoryToZip)).ToString().Length;
zipStream.SetLevel(compressionLevel);
ZipEntry zipEntry = null;
FileStream fileStream = null;
foreach (string fileName in fileList)
{
zipEntry = new ZipEntry(fileName.Remove(0, directoryNameLength));
zipStream.PutNextEntry(zipEntry);
if (!fileName.EndsWith(@"/"))
{
fileStream = File.OpenRead(fileName);
fileStream.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}
}
}
解壓縮類
///
/// 解壓縮類
///
class UnZipClass
{
private byte[] buffer = new byte[2048];
///
/// 建構函式
///
/// 緩衝區大小
public UnZipClass(int bufferSize)
{
buffer = new byte[bufferSize];
}
///
/// 預設建構函式
///
public UnZipClass()
{
}
///
/// 解壓縮檔案
///
/// 壓縮檔案路徑
/// 解壓縮檔案路徑
public void UnZipFile(string zipFilePath, string unZipFilePatah)
{
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry zipEntry = null;
while ((zipEntry=zipStream.GetNextEntry()) != null)
{
string fileName = Path.GetFileName(zipEntry.Name);
if (!string.IsNullOrEmpty(fileName))
{
if (zipEntry.CompressedSize == 0)
break;
using (FileStream stream = File.Create(unZipFilePatah + fileName))
{
while (true)
{
int size = zipStream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
stream.Write(buffer, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
///
/// 解壓縮目錄
///
/// 壓縮目錄路徑
/// 解壓縮目錄路徑
public void UnZipDirectory(string zipDirectoryPath, string unZipDirecotyPath)
{
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipDirectoryPath)))
{
ZipEntry zipEntry = null;
while ((zipEntry = zipStream.GetNextEntry())!= null)
{
string directoryName = Path.GetDirectoryName(zipEntry.Name);
string fileName = Path.GetFileName(zipEntry.Name);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(directoryName);
}
if (!string.IsNullOrEmpty(fileName))
{
if (zipEntry.CompressedSize == 0)
break;
if (zipEntry.IsDirectory)
{
directoryName = Path.GetDirectoryName(unZipDirecotyPath + zipEntry.Name);
Directory.CreateDirectory(directoryName);
}
using (FileStream stream = File.Create(unZipDirecotyPath + zipEntry.Name))
{
while (true)
{
int size = zipStream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
stream.Write(buffer, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-553427/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 利用Java實現zip壓縮/解壓縮 (轉)Java
- JAVA基礎:利用Java實現zip壓縮解壓縮(轉)Java
- aix 下壓縮與解壓縮AI
- AIX 上壓縮與解壓縮AI
- 用ASP實現線上壓縮與解壓縮功能程式碼
- aix 檔案的壓縮與解壓縮AI
- Asp.net實現線上壓縮與解壓ASP.NET
- Python實現壓縮和解壓縮Python
- Linux tar分卷壓縮與解壓縮Linux
- 利用 canvas 實現圖片壓縮Canvas
- 利用 canvas 實現資料壓縮Canvas
- Linux下常用壓縮格式的壓縮與解壓方法Linux
- Linux 常用的壓縮與解壓縮命令詳解Linux
- linux壓縮解壓縮Linux
- tar的打包-壓縮與解壓縮,並解壓到指定的目錄
- 壓縮Word,一鍵實現Word文件壓縮
- C++ MiniZip實現目錄壓縮與解壓C++
- 檔案的壓縮與解壓縮zz--linuxLinux
- tar 分卷壓縮&解壓縮命令
- Linux下常用壓縮格式的壓縮與解壓方法---轉載Linux
- 哈夫曼實現檔案壓縮解壓縮(c語言)C語言
- PAT1078字串壓縮與解壓(java實現)字串Java
- Java實現檔案壓縮與解壓[zip格式,gzip格式]Java
- Linux中檔案的壓縮與解壓縮(轉貼)Linux
- unix和linux下常用壓縮格式的壓縮與解壓方法(轉)Linux
- linux下壓縮解壓縮命令Linux
- Linux壓縮及解壓縮命令Linux
- 字串的壓縮和解壓縮字串
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- linux 高效壓縮工具之xz的壓縮解壓使用Linux
- linux壓縮(解壓縮)命令詳解-轉Linux
- 實用的壓縮解壓工具:WinZip for MacMac
- JAVA壓縮和解壓縮Java
- zip壓縮和解壓縮
- Linux壓縮解壓Linux
- 用 Huffman 樹實現檔案壓縮並解壓
- Linux下的tar壓縮解壓縮命令詳解Linux
- Linux下檔案的壓縮與解壓Linux