c# 上传压缩包(Zip/Rar) 解压,遍历文件夹和文件
|
admin
2025年2月13日 17:15
本文热度 398
|
:c# 上传压缩包(Zip/Rar) 解压,遍历文件夹和文件
public void MuliteImport(string zipFilePath)
{
string unZipDir = string.Format("{0}/{1}/{2}/{3}", Config.GetValue("AnnexesFile"), "Decompress", LoginUserInfo.Get().userId, DateTime.Now.ToString("yyyyMMdd"));
string FileEextension = Path.GetExtension(zipFilePath);
if (!Directory.Exists(unZipDir))
{
Directory.CreateDirectory(unZipDir);
}
if (FileEextension.ToLower() == ".zip")
{
Decompress(unZipDir, zipFilePath);
}
else if (FileEextension.ToLower() == ".rar")
{
DeCompressRar(unZipDir, zipFilePath);
}
}
#region 解压缩文件
public void Decompress(string unZipDir, string zipFilePath)
{
unZipDir = unZipDir + "/zip";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
byte[] data = new byte[2048];
int size = 2048;
ZipEntry theEntry = null;
using (ZipInputStream inputStream = new ZipInputStream(File.OpenRead(zipFilePath)))
{
while ((theEntry = inputStream.GetNextEntry()) != null)
{
if (!string.IsNullOrEmpty(theEntry.Name))
{
string fileName = Path.Combine(unZipDir, theEntry.Name);
fileName = fileName.Replace('/', '\\');
if (fileName.EndsWith("\\"))
{
Directory.CreateDirectory(fileName);
ec_library_catalogueEntity catalogue = new ec_library_catalogueEntity();
continue;
}
using (FileStream streamWriter = File.Create(fileName))
{
while (true)
{
size = inputStream.Read(data, 0, data.Length);
if (size <= 0) break;
streamWriter.Write(data, 0, size);
}
streamWriter.Close();
}
}
}
inputStream.Close();
}
}
public static string ExistsWinRar()
{
string result = string.Empty;
string key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe";
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(key);
if (registryKey != null)
{
result = registryKey.GetValue("").ToString();
registryKey.Close();
}
return result;
}
public void DeCompressRar(string unZipDir, string zipFilePath)
{
var Compressfiles = new List<FileInfo>();
string winrarPath = ExistsWinRar();
if (!string.IsNullOrEmpty(winrarPath))
{
unZipDir = unZipDir + "/rar";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
string winrarDir = Path.GetDirectoryName(winrarPath);
string commandOptions = string.Format("x {0} {1} -y", zipFilePath, unZipDir);
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = Path.Combine(winrarDir, "rar.exe");
processStartInfo.Arguments = commandOptions;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = processStartInfo;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
var files = 0;
while (process.HasExited)
{
files++;
}
process.WaitForExit();
process.Close();
var dir = new DirectoryInfo(unZipDir);
FindFile(dir, Compressfiles);
}
else
{
}
}
public void FindFile(DirectoryInfo di, List<FileInfo> files)
{
FileInfo[] fis = di.GetFiles();
files.AddRange(fis);
DirectoryInfo[] dis = di.GetDirectories();
for (int j = 0; j < dis.Length; j++)
{
FindFile(dis[j], files);
}
}
#endregion
该文章在 2025/2/13 17:16:39 编辑过