Address: http://www.csdn.com.cn/html/040531/200441448014540.html I needed to compress and decompress the file while working on the project, so I downloaded it from the http://www.icsharpcode.net.
Source code for compression and decompression, but after downloading it, there are so manyCodeI don't know how to start.
I was so patient that I finally found a way to study it. Two types of File compression and decompression are rewritten for your own needs,
Zipclass and unzipclass. After encountering a lot of difficulties, I decided to write out compression and decompression.ProgramAfter,
Be sure to share the source code so that the first contact with compressed and decompressed friends 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 ))
{< br> 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)
{
IntSizeread=Streamtozip. Read (buffer,0, Buffer. Length );
Zipstream. Write (buffer,0, Sizeread );
Size+ =Sizeread;
}
}
Catch (System. Exception ex)
{
ThrowEx;
}
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 a compressed file
Filestream FS = File. openread (File );
Byte [] Buffer = New Byte [Fs. Length];
FS. Read (buffer, 0 , Buffer. Length );
Zipentry = 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"; // 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 );
} All right, so far, the compression and decompression classes have been completed. If you need a friend, take the call directly. |