Implement File compression and decompression code in asp.net

Source: Internet
Author: User
Tags crc32

The article summarizes the implementation of File compression and decompression code in asp.net C #. For more information, see this article.

The Code is as follows: Copy code

Using System;
Using System. IO;
Using ICSharpCode. SharpZipLib. Checksums;
Using ICSharpCode. SharpZipLib. Zip;
Using ICSharpCode. SharpZipLib. GZip;
 
Namespace ZipFile
{
/// <Summary>
/// Compressed file
/// </Summary>
Public class ZipHelp
{
Public string ZipName {get; set ;}
/// <Summary>
/// Compressed folder
/// </Summary>
/// <Param name = "zipSourcePath"> path of the folder to be compressed (full path) </param>
/// <Param name = "zipToFilePath"> the path saved after compression must contain a suffix such as: D: \ aa.zip (if it is null, it is saved to the same-level folder by default. The name is the source file name) </param>
Public void ZipFileMain (string zipSourcePath, string zipToFilePath)
{
String [] filenames = Directory. GetFiles (zipSourcePath );
ZipName = zipSourcePath. Substring (zipSourcePath. LastIndexOf ("\") + 1 );
// Define the compressed directory object
Crc32 crc = new Crc32 ();
ZipOutputStream s = new ZipOutputStream (File. Create (zipToFilePath. Equals ("")? ZipSourcePath + ". zip": zipToFilePath ));
 
S. SetLevel (6); // sets the compression level
// Recursively compress all files and word folders in the folder
AddDirToDir (crc, s, zipSourcePath );
 
S. Finish ();
S. Close ();
}
/// <Summary>
/// Compress a single object
/// </Summary>
/// <Param name = "zipSourcePath"> path of the file to be compressed (full path) </param>
/// <Param name = "zipToFilePath"> path of the compressed file (if it is a null character, the file name is the source file name by default compressed to the same directory) </param>
Public void ZipByFile (string zipSourcePath, string zipToFilePath)
{
// Define the compressed directory object
Crc32 crc = new Crc32 ();
String dirName = zipSourcePath. substring (zipSourcePath. lastIndexOf ("\") + 1, zipSourcePath. lastIndexOf (". ")-(zipSourcePath. lastIndexOf ("\") + 1) + ". zip ";
ZipOutputStream s = new ZipOutputStream (File. Create (zipToFilePath. Equals ("")? ZipSourcePath. Substring (0, zipSourcePath. LastIndexOf ("\") + "\" + dirName: zipToFilePath ));
S. SetLevel (6); // sets the compression level
AddFileToDir (crc, s, zipSourcePath, 0 );
S. Finish ();
S. Close ();
}
/// <Summary>
/// Compress a single file to a specified compressed folder (called internally)
/// </Summary>
/// <Param name = "crc"> </param>
/// <Param name = "s"> </param>
/// <Param name = "file"> file path </param>
Public void AddFileToDir (Crc32 crc, ZipOutputStream s, string file, int dotype)
{
FileStream fs = File. OpenRead (file );
Byte [] buffer = new byte [fs. Length];
Fs. Read (buffer, 0, buffer. Length );
String filename = "";
If (dotype = 0)
Filename = file. Substring (file. LastIndexOf ("\") + 1 );
Else
Filename = file. Substring (file. IndexOf (ZipName ));
ZipEntry entry = new ZipEntry (filename );
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 );
}
/// <Summary>
/// Recursive Folder level (Internal call)
/// </Summary>
/// <Param name = "crc"> </param>
/// <Param name = "s"> </param>
/// <Param name = "file"> </param>
Public void AddDirToDir (Crc32 crc, ZipOutputStream s, string file)
{
// Add files in this folder
String [] files = Directory. GetFiles (file );
Foreach (string I in files)
{
AddFileToDir (crc, s, I, 1 );
}
// Query subfolders in this folder
String [] dirs = Directory. GetDirectories (file );
Foreach (string I in dirs)
{
AddDirToDir (crc, s, I );
}
}
}
}


------------------------------------------- Next is the decompressed class ------------------------------------------------------

The Code is as follows: Copy code

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 ZipFile
{
/// <Summary>
/// Decompress the file
/// </Summary>
Public class UnZipFile
{
/// <Summary>
/// Decompress the file
/// </Summary>
/// <Param name = "UnSourceZip"> source file </param>
/// <Param name = "UnZipToPath"> extract the package to the directory path (if it is null, extract it to the current directory) </param>
Public void UnZip (string UnSourceZip, string UnZipToPath)
{
ZipInputStream s = new ZipInputStream (File. OpenRead (UnSourceZip ));
 
ZipEntry theEntry;
While (theEntry = s. GetNextEntry ())! = Null)
{
 
String fileName = Path. GetFileName (theEntry. Name );
 
// Generate the extract directory
If (! UnZipToPath. Equals (""))
Directory. CreateDirectory (UnZipToPath );
Else
UnZipToPath = UnSourceZip. Substring (0, UnSourceZip. LastIndexOf ("\")> 0? UnSourceZip. LastIndexOf ("\"): 0 );
If (fileName! = String. Empty)
{
// Create a folder
Int startIndex = 0;
While (theEntry. Name. IndexOf ("/", startIndex)> 0)
{
// Calculate the folder path
String dirpath = theEntry. Name. Substring (0, theEntry. Name. IndexOf ("/", startIndex ));
// Add a folder
Directory. CreateDirectory (UnZipToPath. Equals ("")? Dirpath: UnZipToPath + "//" + dirpath );
StartIndex = theEntry. Name. IndexOf ("/", startIndex) + 1;
}
// Extract the file to the specified directory
FileStream streamWriter = File. Create (UnZipToPath. Equals ("")? TheEntry. Name: UnZipToPath + "//" + 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 ();
}
}
}

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.