最近,在项目中经常需要处理压缩和解压缩文件的操作。在网上找到了相关资料,自己整理了下,写了两个类:一个压缩类;一个解压缩类。
当然是利用了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博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-553427/ ,如需转载,请注明出处,否则将追究法律责任。