C # Use ICSharpCode. SharpZipLib. dll to compress the file and decompress the file

Source: Internet
Author: User
Tags crc32

These two types are found on the internet, modified, changed to the local, the test passed. The compressed file is. Zip
I feel pretty good, so I will share it with you.
/// <Summary>
/// Compressed file
/// </Summary>

Using System;
Using System. IO;

Using ICSharpCode. SharpZipLib. Checksums;
Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. GZip;

Namespace FtpResume. Utility
{
Public class ZipClass
{
Public string cutStr = "";

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 ();
}

// Get all DirectoryInfo
Private void direct (DirectoryInfo di, ref ZipOutputStream s, Crc32 crc)
{
// DirectoryInfo di = new DirectoryInfo (filenames );
DirectoryInfo [] dirs = di. GetDirectories ("*");

// Traverse all subdirectories under the Directory
Foreach (DirectoryInfo dirNext in dirs)
{
// Add all files in the directory to the ZipOutputStream s compressed stream.
FileInfo [] a = dirNext. GetFiles ();
This. writeStream (ref s, a, crc );

// Recursive call is performed until all directories are traversed.
Direct (dirNext, ref s, crc );
}
}

Private void writeStream (ref ZipOutputStream s, FileInfo [] a, Crc32 crc)
{
Foreach (FileInfo fi in)
{
// String FFN = fi. FullName;
FileStream fs = fi. OpenRead ();

Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );

// ZipEntry entry = new ZipEntry (file); Path. GetFileName (file)
String file = fi. FullName;
File = file. Replace (cutStr ,"");

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 );
}
}

/// <Summary>
/// Compress specified files in a specified directory (including files in subdirectories)
/// </Summary>
/// <Param name = "zippath"> args [0] specifies the path of the directory to be compressed.
/// For example: D :\\ temp \ (note that you can add \ After temp, but you can modify it when writing a program) </param>
/// <Param name = "zipfilename"> args [1] indicates the compressed file name and path.
/// Example: D: \ temp.zip </param>
/// <Param name = "fileFilter"> file filtering, for example, *. xml. Only the. xml file is compressed. </param>
///
Public bool ZipFileMain (string zippath, string zipfilename, string fileFilter)
{
Try
{
// String filenames = Directory. GetFiles (args [0]);

Crc32 crc = new Crc32 ();
ZipOutputStream s = new ZipOutputStream (File. Create (zipfilename ));

S. SetLevel (6); // 0-store only to 9-means best compression

DirectoryInfo di = new DirectoryInfo (zippath );

FileInfo [] a = di. GetFiles (fileFilter );

CutStr = zippath. Trim ();
// Compress all files in this directory
WriteStream (ref s, a, crc );
// Compress the subdirectories and their files in this directory
Direct (di, ref s, crc );

S. Finish ();
S. Close ();
}
Catch
{
Return false;
}
Return true;
}
}
}

 

/// <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 FtpResume. Utility
{
Public class UnZipClass
{
/// <Summary>
/// Decompress the file (the compressed file contains subdirectories)
/// </Summary>
/// <Param name = "zipfilepath"> path of the file to be decompressed </param>
/// <Param name = "unzippath"> decompress the package to the specified directory </param>
Public void UnZip (string zipfilepath, string unzippath)
{
ZipInputStream s = new ZipInputStream (File. OpenRead (zipfilepath ));

ZipEntry theEntry;
While (theEntry = s. GetNextEntry ())! = Null)
{
String directoryName = Path. GetDirectoryName (unzippath );
String fileName = Path. GetFileName (theEntry. Name );

// Generate the extract directory
Directory. CreateDirectory (directoryName );

If (fileName! = String. Empty)
{
// If the file size is 0 after compression, it indicates that the file is empty, so no read and write operations are required.
If (theEntry. CompressedSize = 0)
Break;
// Extract the file to the specified directory
DirectoryName = Path. GetDirectoryName (unzippath + theEntry. Name );
// Create the following directories and subdirectories
Directory. CreateDirectory (directoryName );

FileStream streamWriter = File. Create (unzippath + 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 ();
}
}
}

 

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.