C#怎么解压同时包含文件和文件夹的ZIP
有人知道吗,能告诉下吗 --------------------编程问答-------------------- 我写了一个小方法,不管压缩文件(必须是zip)里有多少文件和文件夹,都可以解压出来,发出来你看看吧首先是上传文件,fs是文件字节流,fileName是文件名称
public string UploadFile(byte[] fs, string fileName)
{
try
{
string path = Server.MapPath(".") + "\\UploadFile\\" + fileName;
path = path.Replace(".zip", "");
//创建解压缩的文件的根目录
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
///定义并实例化一个内存流,以存放提交上来的字节数组。
System.IO.MemoryStream m = new System.IO.MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
fileName = path + "\\" + fileName;
System.IO.FileStream f = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
///把内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
string rootFile = unZipFile(fileName, path);
if (rootFile.Substring(0, 1) == "1")
{
return rootFile.Substring(2);
}
string URL = System.Web.HttpContext.Current.Request["HTTP_HOST"] + System.Web.HttpContext.Current.Request.ApplicationPath +
"/UploadFile";
fileName = fileName.Replace(".zip", "").Substring(fileName.LastIndexOf("\\") + 1);
URL = "http://" + URL + "/" + fileName + "/" + rootFile;
return URL;
}
catch (Exception ex)
{
return "1;" + ex.Message;
}
} --------------------编程问答-------------------- 下面是解压缩的代码
/// <summary>
/// 解压缩文件
/// </summary>
/// <param name="TargetFile">待解压的文件</param>
/// <param name="fileDir">解压出来的文件保存的路径</param>
/// <param name="ErrorMessage"></param>
/// <returns></returns>
public string unZipFile(string TargetFile, string fileDir)
{
string rootFile = "";
try
{
//读取压缩文件(zip文件),准备解压缩
ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
ZipEntry theEntry;
string path = fileDir; //解压出来的文件保存的路径
string rootDir = ""; //根目录下的第一个子文件夹的名称
while ((theEntry = s.GetNextEntry()) != null)
{
rootDir = Path.GetDirectoryName(theEntry.Name); //得到根目录下的第一级子文件夹的名称
if (rootDir.IndexOf("\\") >= 0)
{
rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
}
string dir = Path.GetDirectoryName(theEntry.Name); //根目录下的第一级子文件夹的下的文件夹的名称
string fileName = Path.GetFileName(theEntry.Name); //根目录下的文件名称
if (dir != "" && fileName == "") //创建根目录下的子文件夹,不限制级别
{
if (!Directory.Exists(fileDir + "\\" + dir))
{
path = fileDir + "\\" + dir; //在指定的路径创建文件夹
Directory.CreateDirectory(path);
}
}
else if (dir == "" && fileName != "") //根目录下的文件
{
path = fileDir;
rootFile = fileName;
}
else if (dir != "" && fileName != "") //根目录下的第一级子文件夹下的文件
{
if (dir.IndexOf("\\") > 0) //指定文件保存的路径
{
path = fileDir + "\\" + dir;
}
}
if (dir == rootDir) //判断是不是需要保存在根目录下的文件
{
path = fileDir + "\\" + rootDir;
}
//以下为解压缩zip文件的基本步骤
//基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
if (fileName != String.Empty)
{
FileStream streamWriter = File.Create(path + "\\" + 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;
}
}
streamWriter.Close();
}
}
s.Close();
return rootFile;
}
catch (Exception ex)
{
return "1;" + ex.Message;
}
} --------------------编程问答-------------------- 通过rar的注册信息来解压文件:
压缩:
string the_rar;
Microsoft.Win32.RegistryKey the_Reg;
object the_Obj;
string the_Info;
System.Diagnostics.ProcessStartInfo the_StartInfo;
System.Diagnostics.Process the_Process;
try
{
the_Reg = Microsoft.Win32.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);
the_Info = " a " + " 1.rar " + " " + @"C:\1\1.txt";
the_StartInfo = new System.Diagnostics.ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\1";//获取或设置要启动的进程的初始目录。
the_Process = new System.Diagnostics.Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("压缩成功!");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
解压:
string the_rar;
Microsoft.Win32.RegistryKey the_Reg;
object the_Obj;
string the_Info;
System.Diagnostics.ProcessStartInfo the_StartInfo;
System.Diagnostics.Process the_Process;
try
{
the_Reg = Microsoft.Win32.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);
the_Info = " X " + " 1.rar " + " " + @"C:\1";
the_StartInfo = new System.Diagnostics.ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\1";//获取或设置要启动的进程的初始目录。
the_Process = new System.Diagnostics.Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("解压缩成功");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
} --------------------编程问答-------------------- 下载个ZipForge for .net版的,解、压都可以 --------------------编程问答--------------------
D:\Program Files\怡香工作[code=C#]室\批量获取文件名\[/code]
补充:.NET技术 , C#