Compress/decompress files using C # code

Source: Internet
Author: User
Tags crc32

By referencing a DLL (icsharpcode. dll), you can implement the described functions...

1. compressed files

Using system;
Using icsharpcode. sharpziplib;
Using icsharpcode. sharpziplib. checksums;
Using system. IO;
Using icsharpcode. sharpziplib. Zip;
Using system. collections;

Namespace wsupfiles
{
/// <Summary>
/// Summary of common.
/// </Summary>
Public class common
{
Public common ()
{
//
// Todo: add the constructor logic here
//
}

/// <Summary>
/// Compressed file
/// </Summary>
/// <Param name = "sourcefilenames"> set of compressed file names </param>
/// <Param name = "destfilename"> name of the compressed file </param>
/// <Param name = "password"> password </param>
Public static void zipfile (string path, string destfilename)
{
CRC32 CRC = new CRC32 ();
Zipoutputstream S = new zipoutputstream (file. Create (destfilename ));
S. Password = "";
S. setlevel (6); // 0-store only to 9-means best compression
// Define
System. Io. directoryinfo mydir = new directoryinfo (PATH );
If (mydir. exists = true)
{
System. Io. fileinfo [] myfileary = mydir. getfiles ();

// Extracts information from each file in the folder cyclically,
Foreach (fileinfo objfiles in myfileary)
{
Filestream FS = file. openread (objfiles. fullname );
Byte [] buffer = new byte [fs. Length];
FS. Read (buffer, 0, buffer. Length );
Zipentry entry = new zipentry (objfiles. fullname );
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 );
}
S. Finish ();
S. Close ();
}
}

}
}

To encrypt the compressed file, S. Password = "AAA"; AAA is the password.

Ii. decompress the file

/// <Summary>
/// Decompress the file
/// </Summary>
/// <Param name = "sourcefilename"> name of the extracted file </param>
/// <Param name = "destpath"> decompressed file directory </param>
/// <Param name = "password"> password </param>
Public static void unzipfile (string sourcefilename, string destpath, string filetype)
{
Zipinputstream S = new zipinputstream (file. openread (sourcefilename ));
Zipentry theentry;
Arraylist Al = new arraylist ();

While (theentry = S. getnextentry ())! = NULL)
{
String filename = path. getfilename (theentry. Name );
If (filename! = "")
{
Filename = destpath + "\" + filename;
If (! Directory. exists (destpath ))
{
Directory. createdirectory (destpath );
}
Filestream streamwriter = file. Create (filename );
Int size = 2048;
Byte [] DATA = new byte [2048];
S. Password = "";
While (true)
{
Size = S. Read (data, 0, Data. Length );
If (size> 0)
{
Streamwriter. Write (data, 0, size );
}
Else
{
Break;
}
}
Streamwriter. Close ();
}

}
S. Close ();

}

Note:ProgramThe compressed files to be decompressed by tools on the system will have a considerable number of paths, because they retain the original absolute path during compression, however, the compressed file only contains the compressed target file. The file extracted by the program is the relative file path.


Turn: http://blog.csdn.net/jinru2560/archive/2006/01/12/577493.aspx

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.