Analysis of C # using SharpZipLib method

Source: Internet
Author: User
Tags crc32 datetime header zip

C # language has a lot to learn, here we mainly introduce C # using SharpZipLib, including the introduction of compression and decompression of the two classes, respectively, Zipclass and Unzipclass.

When you need to do the project to compress and decompress files, and then downloaded from the http://www.icsharpcode.net on the compression and decompression of the source code, but download down, face so many codes, temporarily do not know how to start. Had to endure the heart, slowly research, finally found the way. For their own needs to rewrite the file compression and decompression of the two classes, respectively, Zipclass and Unzipclass. One encountered a lot of difficulties, decided to write to compress and decompress the program, the source code must be posted out to share, so that the first contact compression and decompression of friends can take a few detours. The following explains how C # uses SharpZipLib to compress and decompress files.

First, you need to use Sharpziplib.dll in C # in your project. Then modify the class on which to compress and decompress. Implementation of the source code is as follows:

///


<summary>





///Compressed File


///


</summary>





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, the 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);


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.finish ();


Zipstream.close ();


Streamtozip.close ();


}





public void Zipfilemain (string[] args)


{


string[] filenames = Directory.GetFiles (args[0));





CRC32 CRC = New Crc32 ();


Zipoutputstream s = new Zipoutputstream (File.create (args[1));





S.setlevel (6); 0-store to 9-means best compression





foreach (string file in filenames)


{


//Open compressed file


FileStream fs = File.openread (File);





byte[] buffer = new BYTE[FS. Length];


FS. Read (buffer, 0, buffer.) Length);


ZipEntry entry = new ZipEntry (file);





entry.datetime = DateTime.Now;





//Set Size and the CRC, because the information


//about the size and CRC should is stored in the header


//If it is not set it are automatically written in the footer.


//(in this case size = = CRC = 1 in the header)


//Some zip programs have problems with ZIP files, 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 (entry);





s.write (buffer, 0, buffer. Length);





}





S.finish ();


S.close ();


}


}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.