asp.net SharpZipLib的压缩与解压问题
程序员文章站
2024-03-09 16:26:11
我使用sharpziplib.dll中遇到的问题是:利用sharpziplib压缩后生成的*.rar文件,利用其可以正常解压,但如果使用文件右击压缩生成的*.rar文件,在...
我使用sharpziplib.dll中遇到的问题是:利用sharpziplib压缩后生成的*.rar文件,利用其可以正常解压,但如果使用文件右击压缩生成的*.rar文件,在解压过程中出错,具体报错信息:wrong local header signature: 0x21726152 ;但*.zip文件可正常解压。
具体压缩、解压代码实现参照网络上的代码,贴出概要代码:
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourcefilepath">源文件路径</param>
/// <param name="destinationpath">压缩文件后的保存路径</param>
/// <returns>压缩是否成功</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;
//解压缩*.rar文件运行至此处出错:wrong local header signature: 0x21726152,解压*.zip文件不出错
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;
}
如果需解压*.rar的压缩文件在网络也可以找到相关的实现代码,概要代码:
public bool decompressrar(string sourcefilepath, string destinationpath)
{
try
{
string severdir = @"d:\program files\winrar";//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;
}
}
我本想利用fileupload控件将上传的压缩文件解压后保存至相对应的目录并更新数据库文件目录,后发现一些较好的用于上传的开源软件:如neatupload,swfupload可以较方便的实现我的需求,遂没有过多纠缠于sharpziplib,可能关于sharpziplib的压缩与解压有其它用法,不能被我误导,以上代码是从网络上整合出来的,因为它太过于重复和散乱。
具体压缩、解压代码实现参照网络上的代码,贴出概要代码:
复制代码 代码如下:
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourcefilepath">源文件路径</param>
/// <param name="destinationpath">压缩文件后的保存路径</param>
/// <returns>压缩是否成功</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;
//解压缩*.rar文件运行至此处出错:wrong local header signature: 0x21726152,解压*.zip文件不出错
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;
}
如果需解压*.rar的压缩文件在网络也可以找到相关的实现代码,概要代码:
复制代码 代码如下:
public bool decompressrar(string sourcefilepath, string destinationpath)
{
try
{
string severdir = @"d:\program files\winrar";//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;
}
}
我本想利用fileupload控件将上传的压缩文件解压后保存至相对应的目录并更新数据库文件目录,后发现一些较好的用于上传的开源软件:如neatupload,swfupload可以较方便的实现我的需求,遂没有过多纠缠于sharpziplib,可能关于sharpziplib的压缩与解压有其它用法,不能被我误导,以上代码是从网络上整合出来的,因为它太过于重复和散乱。