The problem I'm using in SharpZipLib.dll is that you can extract the *.rar file using SharpZipLib compression, but if you use a file right click to compress the generated *. RAR files, in the decompression process error, specific error information: Wrong local header signature:0x21726152, but *.zip file can be normal decompression.
Specific compression, decompression code implementation refer to the code on the network, post the profile code:
Copy Code code as follows:
<summary>
Compress files
</summary>
<param name= "Sourcefilepath" > source file path </param>
<param name= save path after "DestinationPath" > Compressed file </param>
<returns> Compression Success </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;
Todo
{
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;
Uncompressed *.rar file run to here Error: Wrong local header signature:0x21726152, decompression *.zip file error
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 extract *.rar compressed files on the network can also find the relevant implementation code, profile code:
Copy Code code as follows:
public bool Decompressrar (string Sourcefilepath, String destinationpath)
{
Try
{
String severdir = @ "D:\Program Files\winrar"; Want directory for//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)
{
What do you have?
}
return true;
}
catch (System.Exception)
{
return false;
}
}
I would like to use the FileUpload control to extract the compressed file to the corresponding directory and update the database file directory, and found some good for the upload of open source software: such as neatupload,swfupload can be more convenient to achieve my needs, So not too much entangled in sharpziplib, may be about sharpziplib compression and decompression have other uses, can not be misled by me, the above code is integrated from the network, because it is too repetitive and scattered.