About SharpZipLib compression and decompression

Source: Internet
Author: User

About the implementation code of SharpZipLib compression and decompression, there is a pile of code on the network, all over the world, and even the comments are the same, the same articles are copied to copy ??

I use SharpZipLib. dll is generated after SharpZipLib compression *. the rarfile can be decompressed normally, but if the file is compressed by right-clicking *. the rarfile contains the following error information: Wrong Local header signature: 0x21726152; *. the zip file can be decompressed normally.

For specific compression and decompression code implementation, refer to the Code on the network and paste the summary code:

Compressed file Public bool ZipFile (string dirPath, string zipFilePath, ref string error)
{
Try
{
String [] filenames = Directory. GetFiles (dirPath );
Using (ZipOutputStream zs = new ZipOutputStream (File. Create (zipFilePath )))
{
Zs. SetLevel (9 );
Byte [] buffer = new byte [4096];

Foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry (Path. GetFileName (file ));
Entry. DateTime = DateTime. Now;
Zs. PutNextEntry (entry );

Using (FileStream fs = File. OpenRead (file ))
{
Int sourceBytes;
Do
{
SourceBytes = fs. Read (buffer, 0, buffer. Length );
Zs. Write (buffer, 0, sourceBytes );
}
While (sourceBytes> 0 );
}
}
Zs. Finish ();
Zs. Flush ();
Zs. Close ();
}
Return true;
}
Catch (Exception ex)
{
Return false;
}

}

 

 
Decompress the file (error)
 Public bool DeCompress (string sourceFilePath, string destinationPath)
{
Try
{
Using (ZipInputStream zs = new ZipInputStream (File. OpenRead (sourceFilePath )))
{
ZipEntry entry = null;
// Decompress the *. rarfile. An error occurred while running: Wrong Local header signature: 0x21726152. There is no error in extracting the *. zip file.
While (entry = zs. GetNextEntry ())! = Null)
{
String directoryName = Path. GetDirectoryName (entry. Name );
String fileName = Path. GetFileName (entry. Name );

If (! String. IsNullOrEmpty (fileName ))
{
Using (FileStream streamWriter = File. Create (destinationPath + entry. Name ))
{
Int size = 2048;
Byte [] data = new byte [size];
While (true)
{
Size = zs. Read (data, 0, data. Length );
If (size> 0)
{
StreamWriter. Write (data, 0, size );
}
Else
{
Break;
}
}
}
}
}
}
}
Catch (System. Exception)
{
Return false;
}
Return true;
}

 

If you need to decompress the *. rar compressed file on the network, you can also find the relevant implementation code, the summary code:

Decompress the file (correct) Public bool DeCompressRAR (string sourceFilePath, string destinationPath)
{
Try
{
String SeverDir = @ "D: \ Program Files \ WinRAR"; // directory of rar.exe
Process ProcessDecompression = new Process ();
ProcessDecompression. StartInfo. FileName = SeverDir + "\ rar.exe ";

Directory. CreateDirectory (sourceFilePath );

ProcessDecompression. StartInfo. Arguments = "X" + sourceFilePath + "" + destinationPath;

ProcessDecompression. Start ();

While (! ProcessDecompression. HasExited)
{
// Nothing to do here.
}
Return true;
}
Catch (System. Exception)
{
Return false;
}
}

 

 

I wanted to use the FileUpload control to decompress the uploaded compressed file and save it to the corresponding directory and update the database file directory. Then I found some good open-source software for upload, such as NeatUpload, SWFUpload can easily meet my needs, so it is not too entangled in SharpZipLib. It may be useful for compression and decompression of SharpZipLib and cannot be misled by me, the above code is integrated from the network because it is too repetitive and scattered.

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.