1、如何压缩多个文件如何压缩多个文件/文件夹用例:1.压缩1string folder = Path.Combine(Server.MapPath(), TestCompress);2string file = file1.txt;3string zip = myzip;45SampleCompress.Compress(Path.Combine(folder, file), Path.Combine(folder, zip);2.解压1string folder = Path.Combine(Server.MapPath(), TestCompress);2string file = file1
2、.txt;3string zip = myzip;45SampleCompress.Decompress(Path.Combine(folder, zip),Path.Combine(Path.Combine(folder, zipfolder), file);由代码和使用例子我们可以了解到,Code1 只是支持单个文本文件的压缩/解压, 代码非常简单,但是却实际上却没什么用途,功能太少,只是让你有个初步的认识.下面介绍Code2来实现本文的主题内容.Code2:public class GZip/ / Compress/ / The location of the files to incl
3、ude in the zip file, all files including files in subfolders will be included./ Folder to write the zip file into/ Name of the zip file to writepublic static GZipResult Compress(string lpSourceFolder, string lpDestFolder, string zipFileName)return Compress(lpSourceFolder, *.*, SearchOption.AllDirect
4、ories, lpDestFolder, zipFileName, true);/ / Compress/ / The location of the files to include in the zip file/ Search pattern (ie *.* or *.txt or *.gif) to idendify what files in lpSourceFolder to include in the zip file/ Only files in lpSourceFolder or include files in subfolders also/ Folder to wri
5、te the zip file into/ Name of the zip file to write/ Boolean, true deleted the intermediate temp file, false leaves the temp file in lpDestFolder (for debugging)public static GZipResult Compress(string lpSourceFolder, string searchPattern, SearchOption searchOption, string lpDestFolder, string zipFi
6、leName, bool deleteTempFile)DirectoryInfo di = new DirectoryInfo(lpSourceFolder);FileInfo files = di.GetFiles(*.*, searchOption);return Compress(files, lpSourceFolder, lpDestFolder, zipFileName, deleteTempFile);/ / Compress/ / Array of FileInfo objects to be included in the zip file/ Array of Folder
7、 string/ Base folder to use when creating relative paths for the files/ stored in the zip file. For example, if lpBaseFolder is C:zipTestFiles, and there is a file/ C:zipTestFilesfolder1sample.txt in the files array, the relative path for sample.txt/ will be folder1/sample.txt/ Folder to write the z
8、ip file into/ Name of the zip file to writepublic static GZipResult Compress(FileInfo files, string folders, string lpBaseFolder, string lpDestFolder, string zipFileName)/support compress folderIList list = new List();foreach (FileInfo li in files)list.Add(li);foreach (string str in folders)Director
9、yInfo di = new DirectoryInfo(str);foreach (FileInfo info in di.GetFiles(*.*, SearchOption.AllDirectories)list.Add(info);return Compress(list.ToArray(), lpBaseFolder, lpDestFolder, zipFileName, true);/ / Compress/ / Array of FileInfo objects to be included in the zip file/ Base folder to use when cre
10、ating relative paths for the files/ stored in the zip file. For example, if lpBaseFolder is C:zipTestFiles, and there is a file/ C:zipTestFilesfolder1sample.txt in the files array, the relative path for sample.txt/ will be folder1/sample.txt/ Folder to write the zip file into/ Name of the zip file t
11、o writepublic static GZipResult Compress(FileInfo files, string lpBaseFolder, string lpDestFolder, string zipFileName)return Compress(files, lpBaseFolder, lpDestFolder, zipFileName, true);/ / Compress/ / Array of FileInfo objects to be included in the zip file/ Base folder to use when creating relat
12、ive paths for the files/ stored in the zip file. For example, if lpBaseFolder is C:zipTestFiles, and there is a file/ C:zipTestFilesfolder1sample.txt in the files array, the relative path for sample.txt/ will be folder1/sample.txt/ Folder to write the zip file into/ Name of the zip file to write/ Bo
13、olean, true deleted the intermediate temp file, false leaves the temp file in lpDestFolder (for debugging)public static GZipResult Compress(FileInfo files, string lpBaseFolder, string lpDestFolder, string zipFileName, bool deleteTempFile)GZipResult result = new GZipResult();tryif (!lpDestFolder.Ends
14、With()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)CreateZ
15、ipFile(lpTempFile, lpZipFile, result);/ delete the temp fileif (deleteTempFile)File.Delete(lpTempFile);result.TempFileDeleted = true;catch /(Exception ex4)result.Errors = true;return result;private static void CreateZipFile(string lpSourceFile, string lpZipFile, GZipResult result)byte buffer;int cou
16、nt = 0;FileStream fsOut = null;FileStream fsIn = null;GZipStream gzip = null;/ compress the file into the zip filetryfsOut = new FileStream(lpZipFile, FileMode.Create, FileAccess.Write, FileShare.None);gzip = new GZipStream(fsOut, CompressionMode.Compress, true);fsIn = new FileStream(lpSourceFile, F
17、ileMode.Open, FileAccess.Read, FileShare.Read);buffer = new bytefsIn.Length;count = fsIn.Read(buffer, 0, buffer.Length);fsIn.Close();fsIn = null;/ compress to the zip filegzip.Write(buffer, 0, buffer.Length);result.ZipFileSize = fsOut.Length;result.CompressionPercent = GetCompressionPercent(result.T
18、empFileSize, result.ZipFileSize);catch /(Exception ex1)result.Errors = true;finallyif (gzip != null)gzip.Close();gzip = null;if (fsOut != null)fsOut.Close();fsOut = null;if (fsIn != null)fsIn.Close();fsIn = null;private static void CreateTempFile(FileInfo files, string lpBaseFolder, string lpTempFil
19、e, 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 & file
20、s.Length 0)tryresult.Files = new GZipFileInfofiles.Length;/ open the temp file for writingfsOut = new FileStream(lpTempFile, FileMode.Create, FileAccess.Write, FileShare.None);foreach (FileInfo fi in files)lpFolder = fi.DirectoryName + ;trygzf = new GZipFileInfo();gzf.Index = fileIndex;/ read the so
21、urce file, get its virtual path within the source folderlpSourceFile = fi.FullName;gzf.LocalPath = lpSourceFile;vpSourceFile = lpSourceFile.Replace(lpBaseFolder, string.Empty);vpSourceFile = vpSourceFile.Replace(, /);gzf.RelativePath = vpSourceFile;fsIn = new FileStream(lpSourceFile, FileMode.Open,
22、FileAccess.Read, FileShare.Read);buffer = new bytefsIn.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.Write
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1