C # Unzip or compress a folder
A recent project involves a solution to the problem of compression and decompression in C #, which we share.
The main solution here is to unpack the folder containing the problem.
1 ) Download SharpZipLib.dll, at http: // Www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx has the latest free version, "Assemblies for. NET 1.1,. NET 2.0,. NET CF 1.0,. NET CF 2.0:download [297 KB] "Click Download can download, unzip the inside there are many folders, because different version, I use the FW2.0.
2 reference SharpZipLib.dll, right-click the project in the project - Add Reference - Browse to find the DLL you want to add - Confirm
3 ) rewrite the file compression and decompression of the two classes, the new two class named Zipfloclass.cs,unzipfloclass.cs
The source code is as follows
Using system;using system.data;using system.configuration;using system.web;using system.web.security;using System.web.ui;using system.io;using icsharpcode.sharpziplib.checksums;using icsharpcode.sharpziplib.zip;using icsharpcode.sharpziplib.gzip;///<summary>///Zipfloclass Summary description//</summary>public class ZipFloClass{ public void ZipFile (String strfile, String strzip) {if (strfile[strfile.length-1]! = Path.directoryseparatorc HAR) strfile + = Path.directoryseparatorchar; Zipoutputstream s = new Zipoutputstream (File.create (strzip)); S.setlevel (6); 0-store 9-means Best compression zip (strfile, S, strfile); S.finish (); S.close (); private void Zip (String strfile, Zipoutputstream s, string staticfile) {if (strfile[strfile.length-1]! = Path.directoryseparatorchar) strfile + = Path.directoryseparatorchar; Crc32 CRC = New Crc32 (); string[] Filenames = Directory.getfilesystemEntries (strfile); foreach (string file in filenames) {if (directory.exists (file)) {Zip (file, s , staticfile); } else//otherwise directly compress file {//Open compressed file FileStream fs = file.openread (file); byte[] buffer = new BYTE[FS. Length]; Fs. Read (buffer, 0, buffer. Length); String tempfile = file. Substring (staticfile.lastindexof ("\ \") + 1); ZipEntry entry = new ZipEntry (tempfile); Entry. DateTime = DateTime.Now; Entry. Size = fs. Length; Fs. Close (); Crc. Reset (); Crc. Update (buffer); Entry. CRC = CRC. Value; S.putnextentry (entry); S.write (buffer, 0, buffer.) Length); } } }}
、、、、、、、、、、、、、、、
Using system;using system.data;using system.web;using system.text;using system.collections;using System.IO;using System.diagnostics;using system.runtime.serialization.formatters.binary;using ICSharpCode.SharpZipLib.BZip2; Using icsharpcode.sharpziplib.zip;using icsharpcode.sharpziplib.zip.compression;using Icsharpcode.sharpziplib.zip.compression.streams;using icsharpcode.sharpziplib.gzip;using icsharpcode.sharpziplib.checksums;///<summary>///Unzipfloclass Summary description//</summary>public class unzipfloclass{public string Unzipfile (string targetfile, String filedir) {string rootfile = ""; try {//Read compressed file (zip file), prepare to decompress zipinputstream s = new Zipinputstream (File.openread (targetfile.tr Im ())); ZipEntry Theentry; string path = Filedir; Unzip the file to save the path of string rootdir = ""; The name of the first subfolder under the root directory while (theentry = S.getnextentry ())! =NULL) {RootDir = Path.getdirectoryname (theentry.name); Get the name of the first-level subfolder under the root directory if (rootdir.indexof ("\ \") >= 0) {RootDir = root Dir.substring (0, rootdir.indexof ("\ \") + 1); } String dir = Path.getdirectoryname (theentry.name); The name of the folder under the first-level subfolder under the root directory, string fileName = Path.getfilename (theentry.name); The file name under the root directory if (dir! = "") Create a subfolder under the root directory, without limiting the level {if (! Directory.Exists (Filedir + "\" + dir) {path = Filedir + "\ \" + dir; Creates a folder on the specified path directory.createdirect Ory (path); } } else if (dir = = "" && fileName! = "") file {path = Filedir in root directory; RootFile = FileName; } else if (dir! = "" && fileName! = "") The file {if (dir) under the first-level subfolder under the root directory. IndexOf ("\ \") > 0)//Specify the path to save the file {Path = Filedir + "\ \" + dir; }} if (dir = = RootDir) Determine if the file that needs to be saved in the root directory {path = filedir + "\ \" + RootDir; }//The following is the basic procedure for extracting a ZIP file//The basic idea is to traverse all the files in the compressed file and create an identical file. if (fileName! = String.Empty) {FileStream streamWriter = file.create (path + "\ \" + FileName); 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 (); return rootfile; } catch (Exception ex) {return "1;" + ex. Message; } } }
4 ) Reference, create a new page, add two buttons, add a click event for the button
The source code is as follows
protected void Button1_Click (object sender, EventArgs e) { string[] fileproperties = new string[2]; Fileproperties[0] = "d:\\unzipped\\";//file directory to be compressed fileproperties[1] = "D:\\zip\\a.zip"; Compressed target file zipfloclass Zc = new Zipfloclass (); Zc.zipfile (Fileproperties[0], fileproperties[1]); } protected void button2_click (object sender, EventArgs e) { string[] fileproperties = new string[2]; Fileproperties[0] = "d:\\zip\\b.zip";//files to be decompressed fileproperties[1] = "d:\\unzipped\\";//target directory to be placed after decompression Unzipfloclass UNZC = new Unzipfloclass (); Unzc.unzipfile (Fileproperties[0], fileproperties[1]); }
5 Everything OK, you can test it, I can run it.