[Repost] Use Sharpziplib to compress and decompress in the C #

xiaoxiao2021-03-05  32

I need to compress and decompress the file when I do a project, so I downloaded the source code for compression and unpacking from http://www.icsharpcode.net, but after downloading, face so many code, I don't know how to Start. I have to work with my heart, slowly study, I finally found the way. Two classes of file compression and decompression are rewritten for their own needs, respectively, respectively, respectively. Among them, many difficulties have been encountered, they decided to write out the compressed and unzipped procedures, they must put the source code to share, let the first contact compression and decompression friends can take less detours. Let's explain how to compress and decompress file compression and decompression with Sharpziplib downloaded with http://www.icsharpcode.net.

First, you need to reference Sharpziplib.dll in the project. Then modify the classes about compression and decompression. The realization source code is as follows:

///

/// compressed file ///

Using system; using system.io;

Using ICSHARPCODE.SHARPZIPLIB.CHECKSUMS; Using ICSHARPCODE.SHARPZIPLIB.ZIP; Using ICSHARPCODE.SHARPZIPLIB.GZIP;

namespace Compression {public class ZipClass {public void ZipFile (string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize) {// If the file is not found, then an error if (! System.IO.File.Exists (FileToZip)) {throw new System.IO.FileNotFoundException ( "The specified file" FileToZip "could not be found Zipping aborderd.");} System.IO.FileStream StreamToZip = new System.IO.FileStream (FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.FileStream ZipFile = System.IO.File.Create (ZipedFile); ZipOutputStream ZipStream = new ZipOutputStream (ZipFile); ZipEntry ZipEntry = new ZipEntry ( "ZippedFile"); 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) ; {While (size

Entry.datetime = datetime.now; // set size and the crc, because the information // About the size and crc shop be stored in the header // if it is not set it is automaticly write in the footer. // ( In this case size == crc == -1 in the header) // Some Zip Programs Have Problems with zip files That don't store // The size and crc in the header. entry.size = fs.length; fs. Close (); crc.reset (); crc.Update (buffer); entry.crc = crc.value; s.putnextentry; s.write (buffer, 0, buffer.length);} S.Finish ); S.close ();}}} Now let's take a look at the source code of the decompressed file class

///

/// decompression file ///

Using system.collection; using system.io; using system.diagnostics; using system.runtime.serialization.formatters.binary; using system.data ;. using system.data ;. USING SYSTEM.

using ICSharpCode.SharpZipLib.BZip2; using ICSharpCode.SharpZipLib.Zip; using ICSharpCode.SharpZipLib.Zip.Compression; using ICSharpCode.SharpZipLib.Zip.Compression.Streams; using ICSharpCode.SharpZipLib.GZip;

namespace DeCompression {public class UnZipClass {public void UnZip (string [] args) {ZipInputStream s = new ZipInputStream (File.OpenRead (args [0])); ZipEntry theEntry;! while ((theEntry = s.GetNextEntry ()) = null) {string directoryName = Path.GetDirectoryName (args [1]); string fileName = Path.GetFileName (theEntry.Name); // create decompressed directory Directory.CreateDirectory (directoryName);! if (fileName = String.Empty) { // Unzip file to the specified directory fileStream StreamWriter = file.create (args [1] thentry.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;}}. }}} After the compression and decompression class, it is necessary to call in the form. how? Is it a newbie, will not be called? OK, then look down on how to call in the form.

First put two command buttons in the form (don't tell me you don't put it ~), then write the following source code

///

/// call source ///

Private void button2_click_1 (Object sender, system.eventargs e) {string [] fileproperties = new string [2]; fileproperties [0] = "c: // unzipped //"; // Storage file directory fileproperties [1] = "C: //zip//a.zip"; // Compressed target file zipClass zc = new zipClass (); zc.zipfilemain (fileproperties);}

Private void button2_click (object sender, system.eventargs e) {string [] fileproperties = new string [2]; fileproperties [0] = "c: //zip/test.zip"; // The file to be decompressed FILEPROPERTIES [ 1] = "c: // unzipped //"; // Decompressed target directory unzipClass unzc = new unzipClass (); unzc.unzip (fileproperties);}

转载请注明原文地址:https://www.9cbs.com/read-35787.html

New Post(0)