``C#
//# 包含壓縮包相同文件名的文件
//# 不指定文件夹,
public static String Decompress(string Name)
{
try
{
string path = " ";
//读取压缩文件(zip文件),准备解压缩
ZipInputStream zipInputStream = new ZipInputStream(File.Open(Name, FileMode.Open));
ZipEntry zipEntryFromZippedFile;
//解壓文件不為空(GetNextEntry() 取下一个ZIP文件条目并将该流定位在条目数据的开头,如果沒有則返回null)
while ((zipEntryFromZippedFile = zipInputStream.GetNextEntry()) != null)
{
FileInfo fInfo = new FileInfo(zipEntryFromZippedFile.Name);
string filename = Path.GetFileName(zipEntryFromZippedFile.Name);
//判斷文件夾是否存在zipEntryFromZippedFile.Name找到父目錄
if (!fInfo.Directory.Exists)
{
//創建文件夾 根據
fInfo.Directory.Create();
}
//判斷是否是文件
if (zipEntryFromZippedFile.IsFile)
{
//創建文件
FileStream file = fInfo.Create();
//if (filename == ConfigurationManager.AppSettings["ownerName"])
//{
// path = file.Name;
//}
byte[] bufferFromZip = new byte[zipInputStream.Length];
zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
file.Write(bufferFromZip, 0, bufferFromZip.Length);
file.Close();
}
}
zipInputStream.Close();
return path;
}
catch
{
throw;
}
}
//# 解压zip文件(不包含zip的文件夾,解压到指定路径)
public static String Decompress(string Name, string fileDir)
{
try
{
string path = " ";
//读取压缩文件(zip文件),准备解压缩
ZipInputStream zipInputStream = new ZipInputStream(File.Open(Name, FileMode.Open));
ZipEntry zipEntryFromZippedFile;
//解壓文件不為空(GetNextEntry() 取下一个ZIP文件条目并将该流定位在条目数据的开头,如果沒有則返回null)
while ((zipEntryFromZippedFile = zipInputStream.GetNextEntry()) != null)
{
string filename = Path.GetFileName(zipEntryFromZippedFile.Name);
string dir = Path.GetDirectoryName(zipEntryFromZippedFile.Name);
//string name = ConfigurationManager.AppSettings["name"].Split('.')[0];
string str = " ";
//判斷是否是文件夾
if (zipEntryFromZippedFile.IsDirectory)
{
//判斷是否為解壓縮文件夾
//if (dir == name)
//{
// continue;
//}
str = fileDir + @"\" + dir.Split('\\')[1];
if (!Directory.Exists(str))
{
Directory.CreateDirectory(str);
}
}
//判斷是否是文件
if (zipEntryFromZippedFile.IsFile)
{
//判斷是否為子文件下的文件
//if (dir != name && dir != "")
//{
// str = fileDir + @"\" + dir.Split('\\')[1] + @"\" + filename;
//}
//else
//{
str = fileDir + @"\" + filename;
// }
FileStream file = File.Create(str);
//if (filename == ConfigurationManager.AppSettings["ownerName"])
//{
// path = file.Name;
//}
byte[] bufferFromZip = new byte[zipInputStream.Length];
zipInputStream.Read(bufferFromZip, 0, bufferFromZip.Length);
file.Write(bufferFromZip, 0, bufferFromZip.Length);
file.Close();
}
}
zipInputStream.Close();
return path;
}
catch
{
throw;
}
}
``
标签:解压,文件,zipEntryFromZippedFile,Name,ZIP,zipInputStream,bufferFromZip,string From: https://www.cnblogs.com/lanlan2023/p/17350723.html