接:如何压缩多个文件/文件夹(GZipStream and C#)(上)
public static GZipResult Decompress(string lpSourceFolder, string lpDestFolder, string zipFileName) { return Decompress(lpSourceFolder, lpDestFolder, zipFileName, true, true, null, null, 4096); } public static GZipResult Decompress(string lpSourceFolder, string lpDestFolder, string zipFileName, bool writeFiles, string addExtension) { return Decompress(lpSourceFolder, lpDestFolder, zipFileName, true, writeFiles, addExtension, null, 4096); } public static GZipResult Decompress(string lpSrcFolder, string lpDestFolder, string zipFileName, bool deleteTempFile, bool writeFiles, string addExtension, Hashtable htFiles, int bufferSize) { GZipResult result = new GZipResult(); if (!lpSrcFolder.EndsWith("")) { lpSrcFolder += ""; } if (!lpDestFolder.EndsWith("")) { lpDestFolder += ""; } string lpTempFile = lpSrcFolder + zipFileName + ".tmp"; string lpZipFile = lpSrcFolder + zipFileName; result.TempFile = lpTempFile; result.ZipFile = lpZipFile; string line = null; string lpFilePath = null; string lpFolder = null; GZipFileInfo gzf = null; FileStream fsTemp = null; ArrayList gzfs = new ArrayList(); bool write = false; if (string.IsNullOrEmpty(addExtension)) { addExtension = string.Empty; } else if (!addExtension.StartsWith(".")) { addExtension = "." + addExtension; } // extract the files from the temp file try { fsTemp = UnzipToTempFile(lpZipFile, lpTempFile, result); if (fsTemp != null) { while (fsTemp.Position != fsTemp.Length) { line = null; while (string.IsNullOrEmpty(line) && fsTemp.Position != fsTemp.Length) { line = ReadLine(fsTemp); } if (!string.IsNullOrEmpty(line)) { gzf = new GZipFileInfo(); if (gzf.ParseFileInfo(line) && gzf.Length > 0) { gzfs.Add(gzf); lpFilePath = lpDestFolder + gzf.RelativePath; lpFolder = GetFolder(lpFilePath); gzf.LocalPath = lpFilePath; write = false; if (htFiles == null || htFiles.ContainsKey(gzf.RelativePath)) { gzf.RestoreRequested = true; write = writeFiles; } if (write) { // make sure the folder exists if (!Directory.Exists(lpFolder)) { Directory.CreateDirectory(lpFolder); } // read from fsTemp and write out the file gzf.Restored = WriteFile(fsTemp, gzf.Length, lpFilePath + addExtension, bufferSize); } else { // need to advance fsTemp fsTemp.Position += gzf.Length; } } } } } } catch //(Exception ex3) { result.Errors = true; } finally { if (fsTemp != null) { fsTemp.Close(); fsTemp = null; } } // delete the temp file try { if (deleteTempFile) { File.Delete(lpTempFile); result.TempFileDeleted = true; } } catch //(Exception ex4) { result.Errors = true; } result.FileCount = gzfs.Count; result.Files = new GZipFileInfo[gzfs.Count]; gzfs.CopyTo(result.Files); return result; } private static string ReadLine(FileStream fs) { string line = string.Empty; const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; byte b = 0; byte lf = 10; int i = 0; while (b != lf) { b = (byte)fs.ReadByte(); buffer[i] = b; i++; } line = System.Text.Encoding.Default.GetString(buffer, 0, i - 1); return line; } private static bool WriteFile(FileStream fs, int fileLength, string lpFile, int bufferSize) { bool success = false; FileStream fsFile = null; if (bufferSize == 0 || fileLength < bufferSize) { bufferSize = fileLength; } int count = 0; int remaining = fileLength; int readSize = 0; try { byte[] buffer = new byte[bufferSize]; fsFile = new FileStream(lpFile, FileMode.Create, FileAccess.Write, FileShare.None); while (remaining > 0) { if (remaining > bufferSize) { readSize = bufferSize; } else { readSize = remaining; } count = fs.Read(buffer, 0, readSize); remaining -= count; if (count == 0) { break; } fsFile.Write(buffer, 0, count); fsFile.Flush(); } fsFile.Flush(); fsFile.Close(); fsFile = null; success = true; } catch //(Exception ex2) { success = false; } finally { if (fsFile != null) { fsFile.Flush(); fsFile.Close(); fsFile = null; } } return success; } private static string GetFolder(string lpFilePath) { string lpFolder = lpFilePath; int index = lpFolder.LastIndexOf(""); if (index != -1) { lpFolder = lpFolder.Substring(0, index + 1); } return lpFolder; } private static FileStream UnzipToTempFile(string lpZipFile, string lpTempFile, GZipResult result) { FileStream fsIn = null; GZipStream gzip = null; FileStream fsOut = null; FileStream fsTemp = null; const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int count = 0; try { fsIn = new FileStream(lpZipFile, FileMode.Open, FileAccess.Read, FileShare.Read); result.ZipFileSize = fsIn.Length; fsOut = new FileStream(lpTempFile, FileMode.Create, FileAccess.Write, FileShare.None); gzip = new GZipStream(fsIn, CompressionMode.Decompress, true); while (true) { count = gzip.Read(buffer, 0, bufferSize); if (count != 0) { fsOut.Write(buffer, 0, count); } if (count != bufferSize) { break; } } } catch //(Exception ex1) { result.Errors = true; } finally { if (gzip != null) { gzip.Close(); gzip = null; } if (fsOut != null) { fsOut.Close(); fsOut = null; } if (fsIn != null) { fsIn.Close(); fsIn = null; } } fsTemp = new FileStream(lpTempFile, FileMode.Open, FileAccess.Read, FileShare.None); if (fsTemp != null) { result.TempFileSize = fsTemp.Length; } return fsTemp; } private static int GetCompressionPercent(long tempLen, long zipLen) { double tmp = (double)tempLen; double zip = (double)zipLen; double hundred = 100; double ratio = (tmp - zip) / tmp; double pcnt = ratio * hundred; return (int)pcnt; } } public class GZipFileInfo { public int Index = 0; public string RelativePath = null; public DateTime ModifiedDate; public int Length = 0; public bool AddedToTempFile = false; public bool RestoreRequested = false; public bool Restored = false; public string LocalPath = null; public string Folder = null; public bool ParseFileInfo(string fileInfo) { bool success = false; try { if (!string.IsNullOrEmpty(fileInfo)) { // get the file information string[] info = fileInfo.Split(\',\'); if (info != null && info.Length == 4) { this.Index = Convert.ToInt32(info[0]); this.RelativePath = info[1].Replace("/", ""); this.ModifiedDate = Convert.ToDateTime(info[2]); this.Length = Convert.ToInt32(info[3]); success = true; } } } catch { success = false; } return success; } } public class GZipResult { public GZipFileInfo[] Files = null; public int FileCount = 0; public long TempFileSize = 0; public long ZipFileSize = 0; public int CompressionPercent = 0; public string TempFile = null; public string ZipFile = null; public bool TempFileDeleted = false; public bool Errors = false; }