Asp.net sharpziplib compression and decompression Problems

Source: Internet
Author: User

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.
Specific compression and decompression Code Refer to the Code on the network to paste the summary code: Copy code The Code is as follows: // <summary>
/// Compressed file
/// </Summary>
/// <Param name = "sourcefilepath"> source file path </param>
/// <Param name = "destinationpath"> Save path of the compressed file </param>
/// <Returns> whether compression is successful </returns>
Public bool compress (string sourcefilepath, string destinationpath)
{
Try
{
String [] filenames = directory. getfiles (sourcefilepath );
Using (zipoutputstream Zs = new zipoutputstream (file. Create (destinationpath )))
{
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 ();
}
}
Catch (exception)
{
Return false;
}
Return true;
} 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:Copy codeThe Code is as follows: 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.

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.