项目中用到压缩和解压的功能,在网上查查看,有如下几种,整理一下,供参考 可从www./Opensource/SharpZipLib/Download.aspx获得 需要添加以下引用 using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.GZip;
// 对目标文件夹进行压缩,将压缩结果保存为指定文件 //FilePath存放的是目标文件夹下要压缩的文件BakPath 压缩后文件存放的路径 public class ZipClass
} 在解压缩类中添加如下引用 using ICSharpCode.SharpZipLib.Zip;
public void UnZip(string[] args) { ZipInputStream s = new ZipInputStream(File.OpenRead(args[0])); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(args[1]); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { //解压文件到指定的目录 FileStream streamWriter = File.Create(args[1] + theEntry.Name); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } Else { break; } } streamWriter.Close(); } } s.Close(); }
using System.IO.Compression; public void CompressFile(string sourceFile, string destinationFile) { if (File.Exists(sourceFile) == false) //判断文件是否存在 throw new FileNotFoundException(); //创建文件流和字节数组 byte[] buffer = null; FileStream sourceStream = null; FileStream destinationStream = null; GZipStream compressedStream = null; try { sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); buffer = new byte[sourceStream.Length]; //把文件流存放到字节数组中 int checkCounter = sourceStream.Read(buffer, 0, buffer.Length); if (checkCounter != buffer.Length) { throw new ApplicationException(); } destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write); //创建GzipStream实例,写入压缩的文件流 compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true); compressedStream.Write(buffer, 0, buffer.Length); } catch (ApplicationException ex) { MessageBox.Show(ex.Message, "压缩文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { // Make sure we allways close all streams if (sourceStream != null) { sourceStream.Close();} if (compressedStream != null) { compressedStream.Close();} if (destinationStream != null) { destinationStream.Close();} } }
public void DecompressFile(string sourceFile, string destinationFile) { if (File.Exists(sourceFile) == false) throw new FileNotFoundException(); FileStream sourceStream = null; FileStream destinationStream = null; GZipStream decompressedStream = null; byte[] quartetBuffer = null; try { sourceStream = new FileStream(sourceFile, FileMode.Open); decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true); quartetBuffer = new byte[4]; int position = (int)sourceStream.Length - 4; sourceStream.Position = position; sourceStream.Read(quartetBuffer, 0, 4); sourceStream.Position = 0; int checkLength = BitConverter.ToInt32(quartetBuffer, 0); byte[] buffer = new byte[checkLength + 100]; int offset = 0; int total = 0; // 从字节数组中读取压缩数据 while (true) { int bytesRead = decompressedStream.Read(buffer, offset, 100); if (bytesRead == 0) break; offset += bytesRead; total += bytesRead; } destinationStream = new FileStream(destinationFile, FileMode.Create); destinationStream.Write(buffer, 0, total); destinationStream.Flush(); } catch (ApplicationException ex) { MessageBox.Show(ex.Message, "解压文件时发生错误:", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (sourceStream != null) { sourceStream.Close();} if (decompressedStream != null) { decompressedStream.Close();} if (destinationStream != null) { destinationStream.Close();} } } 比较: 引用 SharpZipLib的方法通用性比较强,能压缩和对别的解压文件进行解压; 用GzipStream类对文件进行压缩和解压比较方便,但是其只能对自己的压缩文件进行解压,对常见的压缩格式不支持 |
|
来自: 悟静 > 《.net和asp.net》