解压文件总是报错
//解压类
public class UnZipClass
{
//// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder)
{
if (!File.Exists(FileToUpZip))
{
return;
} if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
} ZipInputStream s = null;
ZipEntry theEntry = null; string fileName;
FileStream fs = null;
FileStream streamWriter = null;
try
{
fs = new FileStream(FileToUpZip,FileMode.Open);//fs.length 引发异常
s = new ZipInputStream(fs);
while ((theEntry = s.GetNextEntry()) != null)//报错 找不到实例
{
if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
/**/
///判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
} streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
catch (Exception e)
{ throw;
} finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if(fs!=null)
{
fs.Close();
fs = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
}
“/EstanPage”应用程序中的服务器错误。Wrong Local header signature: 0x21726152说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: ICSharpCode.SharpZipLib.Zip.ZipException: Wrong Local header signature: 0x21726152源错误:
行 311: catch (Exception e)
行 312: {
行 313: throw;
行 314: }
行 315:源文件: D:\g_工作文件夹\Estan\Estan\EstanBLL\Smart\ZipFile.cs 行: 313
//解压类
public class UnZipClass
{
//// <summary>
/// 解压功能(解压压缩文件到指定目录)
/// </summary>
/// <param name="FileToUpZip">待解压的文件</param>
/// <param name="ZipedFolder">指定解压目标目录</param>
public static void UnZip(string FileToUpZip, string ZipedFolder)
{
if (!File.Exists(FileToUpZip))
{
return;
} if (!Directory.Exists(ZipedFolder))
{
Directory.CreateDirectory(ZipedFolder);
} ZipInputStream s = null;
ZipEntry theEntry = null; string fileName;
FileStream fs = null;
FileStream streamWriter = null;
try
{
fs = new FileStream(FileToUpZip,FileMode.Open);//fs.length 引发异常
s = new ZipInputStream(fs);
while ((theEntry = s.GetNextEntry()) != null)//报错 找不到实例
{
if (theEntry.Name != String.Empty)
{
fileName = Path.Combine(ZipedFolder, theEntry.Name);
/**/
///判断文件路径是否是文件夹
if (fileName.EndsWith("/") || fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
continue;
} streamWriter = File.Create(fileName);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
catch (Exception e)
{ throw;
} finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
if(fs!=null)
{
fs.Close();
fs = null;
}
if (theEntry != null)
{
theEntry = null;
}
if (s != null)
{
s.Close();
s = null;
}
GC.Collect();
GC.Collect(1);
}
}
}
“/EstanPage”应用程序中的服务器错误。Wrong Local header signature: 0x21726152说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: ICSharpCode.SharpZipLib.Zip.ZipException: Wrong Local header signature: 0x21726152源错误:
行 311: catch (Exception e)
行 312: {
行 313: throw;
行 314: }
行 315:源文件: D:\g_工作文件夹\Estan\Estan\EstanBLL\Smart\ZipFile.cs 行: 313
/// 压缩方法
/// </summary>
/// <param name="patch">预压缩的路径</param>
/// <param name="rarPatch">压缩后的路径</param>
/// <param name="rarName">压缩文件名</param>
public static void RARsave(string patch, string rarPatch, string rarName)
{
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\\WinRAR.exe\\Shell\\Open\\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
Directory.CreateDirectory(patch);
//命令参数 //the_Info = " a " + rarName + " " + @"C:Test?70821.txt"; //文件压缩 the_Info = " a " + rarName + " " + patch + " -r"; ;
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//打包文件存放目录 the_StartInfo.WorkingDirectory = rarPatch;
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
the_Process.WaitForExit();
the_Process.Close();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
///解 压缩方法
/// </summary>
/// <param name="patch">预解压缩的路径</param>
/// <param name="rarPatch">解压缩后的路径</param> public static int unRAR(string unRarPatch, string rarPatch)
{ try
{
System.Diagnostics.Process Process1 = new System.Diagnostics.Process();
Process1.StartInfo.FileName = "Winrar.exe";
Process1.StartInfo.CreateNoWindow = true;
Process1.StartInfo.Arguments = " e " + unRarPatch + " " + rarPatch + " -y";
Process1.Start();
Process1.WaitForExit();
if (Process1.HasExited)
{
return Process1.ExitCode;
}
return -1000;
}
catch (Exception ex)
{
return -1001;
}
}
参考