private static void CreateZipFile(string lpSourceFile, string lpZipFile, GZipResult result) { byte[] buffer; int count = 0; FileStream fsOut = null; FileStream fsIn = null; GZipStream gzip = null; // compress the file into the zip file try { fsOut = new FileStream(lpZipFile, FileMode.Create, FileAccess.Write, FileShare.None); gzip = new GZipStream(fsOut, CompressionMode.Compress, true); fsIn = new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); buffer = new byte[fsIn.Length]; count = fsIn.Read(buffer, 0, buffer.Length); fsIn.Close(); fsIn = null; // compress to the zip file gzip.Write(buffer, 0, buffer.Length); result.ZipFileSize = fsOut.Length; result.CompressionPercent = GetCompressionPercent(result.TempFileSize, result.ZipFileSize); } 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; } } }
/// <summary> /// 压缩文件 /// </summary> /// <param name="files">在zip文件中包含的FileInfo对象数组</param> /// <param name="lpBaseFolder"> /// 基础文件夹,在创建的zip文件中存储的文件的相对路径。例如, 如果lpBaseFolder 是 'C:\zipTest\Files\', /// 当存在一个文件 'C:\zipTest\Files\folder1\sample.txt' 在数组'files'里面, 则 sample.txt 的相对路径是 'folder1/sample.txt' /// </param> /// <param name="lpDestFolder">写入Zip文件的文件夹</param> /// <param name="zipFileName">Zip文件名</param> /// <param name="deleteTempFile">布尔值,如果为true则删除中间临时文件,false则在lpDestFolder保留临时文件(调试用)</param> public static GZipResult Compress(FileInfo[] files, string lpBaseFolder, string lpDestFolder, string zipFileName, bool deleteTempFile) { GZipResult result = new GZipResult(); try { if (!lpDestFolder.EndsWith("\\")) { lpDestFolder += "\\"; } string lpTempFile = lpDestFolder + zipFileName + ".tmp"; string lpZipFile = lpDestFolder + zipFileName; result.TempFile = lpTempFile; result.ZipFile = lpZipFile; if (files != null && files.Length > 0) { CreateTempFile(files, lpBaseFolder, lpTempFile, result); if (result.FileCount > 0) { CreateZipFile(lpTempFile, lpZipFile, result); } // delete the temp file if (deleteTempFile) { File.Delete(lpTempFile); result.TempFileDeleted = true; } } } catch //(Exception ex4) { result.Errors = true; } return(result); }
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); }
/// <summary> /// 解压文件 /// </summary> /// <param name="lpSrcFolder">zip文件的源目录</param> /// <param name="lpDestFolder">解压到的目录</param> /// <param name="zipFileName">Zip文件名</param> /// <param name="deleteTempFile">布尔值,如果为true则删除中间临时文件,false则在lpDestFolder保留临时文件(调试用)</param> /// <param name="writeFiles"></param> /// <param name="addExtension">增加后缀名</param> /// <param name="htFiles"></param> /// <param name="bufferSize"></param> /// <returns></returns> 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 void CreateTempFile(FileInfo[] files, string lpBaseFolder, string lpTempFile, GZipResult result) { byte[] buffer; int count = 0; byte[] header; string fileHeader = null; string fileModDate = null; string lpFolder = null; int fileIndex = 0; string lpSourceFile = null; string vpSourceFile = null; GZipFileInfo gzf = null; FileStream fsOut = null; FileStream fsIn = null; if (files != null && files.Length > 0) { try { result.Files = new GZipFileInfo[files.Length]; // open the temp file for writing fsOut = new FileStream(lpTempFile, FileMode.Create, FileAccess.Write, FileShare.None); foreach (FileInfo fi in files) { lpFolder = fi.DirectoryName + "\\"; try { gzf = new GZipFileInfo(); gzf.Index = fileIndex; // read the source file, get its virtual path within the source folder lpSourceFile = fi.FullName; gzf.LocalPath = lpSourceFile; vpSourceFile = lpSourceFile.Replace(lpBaseFolder, string.Empty); vpSourceFile = vpSourceFile.Replace("\\", "/"); gzf.RelativePath = vpSourceFile; fsIn = new FileStream(lpSourceFile, FileMode.Open, FileAccess.Read, FileShare.Read); buffer = new byte[fsIn.Length]; count = fsIn.Read(buffer, 0, buffer.Length); fsIn.Close(); fsIn = null; fileModDate = fi.LastWriteTimeUtc.ToString(); gzf.ModifiedDate = fi.LastWriteTimeUtc; gzf.Length = buffer.Length; fileHeader = fileIndex.ToString() + "," + vpSourceFile + "," + fileModDate + "," + buffer.Length.ToString() + "\n"; header = Encoding.Default.GetBytes(fileHeader); fsOut.Write(header, 0, header.Length); fsOut.Write(buffer, 0, buffer.Length); fsOut.WriteByte(10); // linefeed gzf.AddedToTempFile = true; // update the result object result.Files[fileIndex] = gzf; // increment the fileIndex fileIndex++; } catch //(Exception ex1) { result.Errors = true; } finally { if (fsIn != null) { fsIn.Close(); fsIn = null; } } if (fsOut != null) { result.TempFileSize = fsOut.Length; } } } catch //(Exception ex2) { result.Errors = true; } finally { if (fsOut != null) { fsOut.Close(); fsOut = null; } } } result.FileCount = fileIndex; }