压缩及解压缩文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | using System; using ICSharpCode.SharpZipLib.Zip; using System.IO; namespace Zip { /// <summary> /// 压缩 /// </summary> public class MyCompress { private static readonly object compressLock = new object (); private static readonly object uncompressLock = new object (); /// <summary> /// 压缩文件 /// </summary> /// <param name="FileToZip">要压缩的文件</param> /// <param name="ZipedPath">压缩后文件路径</param> /// <param name="CompressionLevel">压缩级别</param> /// <param name="BlockSize">文件块大小</param> public static void ZipFile( string FileToZip, string ZipedPath, int CompressionLevel, int BlockSize) { lock (compressLock) { //如果文件没有找到,则报错 if (!System.IO.File.Exists(FileToZip)) { throw new System.IO.FileNotFoundException( "The specified file " + FileToZip + " could not be found. Zipping aborderd" ); } #region 压缩后文件存放路径 string fileName; string ZipedFile; fileName = FileToZip.Substring(FileToZip.LastIndexOf( "\\" ) + 1); ZipedFile = ZipedPath + "\\" + fileName + ".zip" ; if (!Directory.Exists(ZipedPath)) Directory.CreateDirectory(ZipedPath); #endregion using (System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile)) { using (ZipOutputStream ZipStream = new ZipOutputStream(ZipFile)) { ZipEntry ZipEntry = new ZipEntry(fileName); ZipEntry.DateTime = ( new System.IO.FileInfo(FileToZip)).LastWriteTime; ZipStream.PutNextEntry(ZipEntry); ZipStream.SetLevel(CompressionLevel); byte [] buffer = new byte [BlockSize]; System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, size); try { while (size < StreamToZip.Length) { int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length); ZipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (System.Exception ex) { throw ex; } ZipStream.Close(); ZipStream.Dispose(); } ZipFile.Close(); ZipFile.Dispose(); } StreamToZip.Close(); StreamToZip.Dispose(); } } } /// <summary> /// 压缩某个目录下的所有文件(如果文件夹不存在将会抛出异常) /// </summary> /// <param name="dir"></param> public static void ZipFile( string dir, string ZipedPath) { if (Directory.Exists(dir)) { string [] Files = Directory.GetFiles(dir); foreach ( string file in Files) { try { ZipFile(file, ZipedPath, 7, 16384); } catch { GlobalRuntime.AddRunLog(file+ "在压缩的过程中出错!" ); continue ; } } } else throw new Exception( "需要进行压缩的文件夹不存在!" ); } /// <summary> /// 解压指定路径下的所有压缩文件到(\\temp)下 /// </summary> /// <param name="dir"></param> public static void UnZip( string dir) { lock (uncompressLock) { if (Directory.Exists(dir)) { string [] Files = Directory.GetFiles(dir); foreach ( string file in Files) { try { UnZip(file, ( new FileInfo(file)).Directory + "\\temp" ); } catch { GlobalRuntime.AddRunLog(file + "在解压过程中出错!" ); continue ; } } } else throw new Exception( "需要进行解压的文件夹不存在!" ); } } /// <summary> /// 解压文件 /// </summary> /// <param name="ZipFile">要解压的文件</param> /// <param name="unZipFilePath">解压后存放的路径</param> public static void UnZip( string zipFile, string unZipFilePath) { lock (uncompressLock) { using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null ) { string directoryName = Path.GetDirectoryName(unZipFilePath); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录 Directory.CreateDirectory(directoryName); if (fileName != String.Empty) { //解压文件到指定的目录 using (FileStream streamWriter = File.Create(unZipFilePath + theEntry.Name)) { int size = 16384; byte [] data = new byte [16384]; while ( true ) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break ; } } streamWriter.Close(); streamWriter.Dispose(); } } } s.Close(); s.Dispose(); } } } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步