I need to compress and decompress the file when doing the project, so I downloaded the source code about compression and decompression from the http://www.icsharpcode.net, but after downloading it, in the face of so much code, I don't know how to start. I was so patient that I finally found a way to study it. The two classes for File compression and decompression are rewritten for their own needs, namely zipclass and unzipclass. After encountering a lot of difficulties, I decided to write the program for compression and decompression, and I must paste the source code for sharing, so that the first contact with compression and decompression can take less detours. The following explains how to use sharpziplib downloaded by the http://www.icsharpcode.net in C # To Compress and decompress the file.
First, you must reference sharpziplib. dll in the project. Then modify the compression and decompression class. 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, an error is returned.
If (! System. Io. file. exists (filetozip ))
{
Throw new system. Io. filenotfoundexception ("the specified file" + filetozip + "cocould 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 = 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 only to 9-means best compression
Foreach (string file in filenames)
{
// Open the 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 shocould be stored in the header
// If it is not set it is automatically written 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 (entry );
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.
/// <Summary>
/// Decompress the file
/// </Summary>
Using system;
Using system. text;
Using system. collections;
Using system. IO;
Using system. diagnostics;
Using system. runtime. serialization. formatters. Binary;
Using system. Data;
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 );
// Generate the extract directory
Directory. createdirectory (directoryname );
If (filename! = String. Empty)
{
// Extract the file to the specified directory
Filestream streamwriter = file. Create (ARGs [1] + 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 ();
}
}
}
With the compression and decompression class, you need to call it in the form. Why? Is it a newbie and cannot be called? OK, and then let's see how to call it in the form.
First, place two command buttons in the form (don't tell me you won't put them ~), Then write the following source code
/// <Summary>
/// Call the source code
/// </Summary>
Private void button2_click_1 (Object sender, system. eventargs E)
{
String [] fileproperties = new string [2];
Fileproperties [0] = "C: \ unzipped \"; // directory of the file to be compressed
Fileproperties [1] = "C: \ zip \ a.zip"; // the target file after compression
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"; // files to be decompressed
Fileproperties [1] = "C: \ unzipped \"; // the directory to which the file is stored after decompression
Unzipclass unzc = new unzipclass ();
Unzc. Unzip (fileproperties );
}