Using system;using system.collections.generic;using system.io;using icsharpcode.sharpziplib.checksums;using Icsharpcode.sharpziplib.zip;namespace buildassetbundle{public class Ziputility {//<summary> All file caches//</summary> list<string> files = new list<string> (); <summary>////All empty directory Caches///</summary> list<string> paths = new List<str Ing> (); <summary>///Compress individual files///</summary>//<param name= "Filetozip" > Files to compress < ;/param>//<param name= "Zipedfile" > full file name after compression </param>//<param name= "Compressionleve L "> compression, Range 0-9, the greater the value, the higher the compression program </param>//<param name=" blockSize "> block size </param> public V OID ZipFile (String filetozip, string zipedfile, int compressionlevel, int blockSize) {if (! System.IO.File.Exists (Filetozip))//If the file is not foundError {throw new FileNotFoundException ("The specified file" + Filetozip + "could not be fou nd. Zipping Aborderd "); } FileStream streamtozip = new FileStream (Filetozip, FileMode.Open, FileAccess.Read); FileStream ZipFile = file.create (zipedfile); Zipoutputstream ZipStream = new Zipoutputstream (ZipFile); ZipEntry zipentry = new ZipEntry (filetozip); Zipstream.putnextentry (ZipEntry); Zipstream.setlevel (CompressionLevel); byte[] buffer = new Byte[blocksize]; int size = streamtozip.read (buffer, 0, buffer. Length); Zipstream.write (buffer, 0, size); try {while (size < streamtozip.length) {int sizeread = St Reamtozip.read (buffer, 0, buffer.) Length); Zipstream.write (buffer, 0, sizeread); Size + = Sizeread; }} CATCH (Exception ex) {GC. Collect (); Throw ex; } zipstream.finish (); Zipstream.close (); Streamtozip.close (); Gc. Collect (); }///<summary>//Compressed directory (including subdirectories and all files)///</summary>//<param name= "root Path > root directory to compress </param>//<param name= "DestinationPath" > Save path </param>//<param Name= "Compresslevel" > compression, Range 0-9, the greater the value, the higher the compression program </param> public void Zipfilefromdirectory (String rootpath, St Ring DestinationPath, int compresslevel) {getalldirectories (RootPath); /* while (rootpath.lastindexof ("\ \") + 1 = = rootpath.length)//Check if the path ends with "\" {RootPath = Rootpath.s Ubstring (0, rootpath.length-1);//If it is, remove the end of the "\"} *///string Rootmark = Rootpath.subst Ring (0, rootpath.lastindexof ("\ \") + 1);//Gets the position of the current path in case the compressed content is convertedinto a relative path. String Rootmark = RootPath + "\";//Gets the position of the current path in case of compression to convert the compressed content into a relative path. Crc32 CRC = New Crc32 (); Zipoutputstream outputstream = new Zipoutputstream (File.create (DestinationPath)); Outputstream.setlevel (Compresslevel); 0-store-9-means Best compression foreach (string file in files) {Fi Lestream FileStream = file.openread (file);//Open compressed file byte[] buffer = new Byte[filestream.length]; FileStream.Read (buffer, 0, buffer.) Length); ZipEntry entry = new ZipEntry (file. Replace (Rootmark, String. Empty)); Entry. DateTime = DateTime.Now; Entry. Size = Filestream.length; Filestream.close (); Crc. Reset (); Crc. Update (buffer); Entry. CRC = CRC. Value; Outputstream.putnextentry (entry); Outputstream.write (buffer, 0, buffer.) Length); } This.files.Clear (); foreach (String Emptypath in Paths) {ZipEntry entry = new ZipEntry (Emptypath.replace (Rootmark, String. Empty) + "/"); Outputstream.putnextentry (entry); } this.paths.Clear (); Outputstream.finish (); Outputstream.close (); Gc. Collect (); }///<summary>//Get all files and folders under the catalogue, and deposit them with paths///</summary>//<par Am Name= "RootPath" > root directory </param> private void GetAllDirectories (string rootpath) {Strin g[] subpaths = directory.getdirectories (RootPath);//Get All subdirectories foreach (string path in subpaths) { GetAllDirectories (path);//The same operation as the root directory for each word directory: The subdirectory is found and the file name of the current directory is saved to list} string[] files = Directory.GetFiles (RootPath); foreach (string file in files) {this.files.Add (file);//The full file name in the current directory is storedFile List} if (subpaths.length = = files. Length && files. Length = = 0)//If empty directory {THIS.PATHS.ADD (rootpath);//Record Empty Directory}}//< ;summary>///Unzip file (contains subdirectories in compressed file)///</summary>//<param name= "Zipfilepath" > to be unpacked Shrink file path </param>///<param Name= "Unzippath" > Extract to specified directory </param>//<returns> extracted file columns Table </returns> Public list<string> UnZip (string zipfilepath, String unzippath) {//Extract out List of files to list<string> unzipfiles = new list<string> (); Check if the output directory ends with "\ \" if (Unzippath. EndsWith ("\ \") = = False | | Unzippath. EndsWith (": \ \") = = False) {Unzippath + = "\"; } zipinputstream s = new Zipinputstream (File.openread (Zipfilepath)); ZipEntry Theentry; while ((Theentry = S.getnextentry ()) = null) {String directoryname = Path.getdirectoryname (Unzippath); String fileName = Path.getfilename (theentry.name); Generate Extract Directory "if the user extracts to the hard drive root directory, you do not need to create the" if (!string. IsNullOrEmpty (directoryname)) {directory.createdirectory (directoryname); } if (FileName! = String.Empty) {//If the file has a compressed size of 0 Then the file is empty, so there is no need to read the write if (theentry.compressedsize = = 0) continue; Unzip the file to the specified directory directoryname = path.getdirectoryname (Unzippath + theentry.name); Create the following directories and subdirectories Directory.CreateDirectory (directoryname); Records the exported file Unzipfiles.add (Unzippath + theentry.name); FileStream streamWriter = file.create (Unzippath + 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 (); Gc. Collect (); return unzipfiles; } public string Getzipfileextention (string filefullname) {int index = Filefullname.lastindexof (" ."); if (index <= 0) {throw new Exception ("The source package file was not a compress file"); }//extension String String ext = filefullname.substring (index); if (ext = = ". rar" | | ext = = ". Zip") { return ext; } else {throw new Exception ("The source package file was not a compress file"); } } }}
static string Zipressourcepath {get {foreach (String arg in System.en Vironment. GetCommandLineArgs ()) {UnityEngine.Debug.Log ("Zipressourcepath:" + arg); if (Arg. StartsWith ("project")) {return arg. Split ('-') [1]; }} return ""; }} static string Zipresdespath {get {foreach (String arg in S Ystem. Environment.getcommandlineargs ()) {UnityEngine.Debug.Log ("Zipresdespath:" + arg); if (Arg. StartsWith ("project")) {return arg. Split ('-') [2]; }} return ""; }} public static void Multifilescompress () {ziputility zip = new ziputility (); DAtetime d = DateTime.Now; String zipname = @ZipResDesPath + d.tostring ("yyyy_mm_dd_hh_mm_ss") + ". zip"; Zip. Zipfilefromdirectory (@ZipResSourcePath, Zipname, 4); UnityEngine.Debug.LogError ("Ziptest Complete:"); }
Features of C # Zip compressed files