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.