[英].NET 3.5 how can I decompress a ZIP file with DotNetZip to a specified location
I have the following that works great if .NET 4.5 is installed 如果安装了.NET 4.5,我的以下软件效果很好
public static void SearchZippedLogs()
{
Console.WriteLine("Searching compressed logs");
Console.WriteLine(" ");
DirectoryInfo di = new DirectoryInfo(sPath + "\\" + sDate + "\\");
FileInfo[] files = di.GetFiles("*.zip");
try
{
foreach (var file in files)
{
using (ZipArchive archive = ZipFile.OpenRead(sPath + "\\" + sDate + "\\" + file.Name))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".ininlog", StringComparison.OrdinalIgnoreCase))
{
try
{
Directory.CreateDirectory(temp);
entry.ExtractToFile(Path.Combine(temp, entry.FullName));
try
{
Stream sTemp = new FileStream(temp + "\\" + entry.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (StreamReader sr = new StreamReader(sTemp))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(sTerm))
{
Console.WriteLine("{0} contains\"{1}\"", file.Name, sTerm);
if (bLog)
Logger(file.Name + " contains " + sTerm, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
break;
}
}
}
}
catch (UnauthorizedAccessException ae)
{
Console.WriteLine(ae.Message);
if (bLog)
Logger(ae.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (SystemException se)
{
Console.WriteLine(se.Message);
if (bLog)
Logger(se.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (ApplicationException ape)
{
Console.WriteLine(ape.Message);
if (bLog)
Logger(ape.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (bLog)
Logger(e.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
}
catch (UnauthorizedAccessException ae)
{
Console.WriteLine(ae.Message);
if (bLog)
Logger(ae.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (SystemException se)
{
Console.WriteLine(se.Message);
if (bLog)
Logger(se.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (ApplicationException ape)
{
Console.WriteLine(ape.Message);
if (bLog)
Logger(ape.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
if (bLog)
Logger(e.Message, Environment.GetEnvironmentVariable("COMPUTERNAME") + "-" + sTerm);
}
finally
{
Directory.Delete(temp, true);
archive.Dispose();
}
}
}
}
}
}
The problem is on our servers are only running .NET 3.5 due to software we run, I need to accomplish the same thing using .NET 3.5 问题是由于我们运行的软件,我们的服务器上仅运行.NET 3.5,我需要使用.NET 3.5完成相同的操作
I have looked at DotNetZip and DeflateStream Class but am having troubles replicating this to .NET 3.5 我看过DotNetZip和DeflateStream类,但在将其复制到.NET 3.5时遇到麻烦
Expected outcome Extract each zip file in the directory to a temporary location, search through it for a item, if found print the filename then delete the temporary items 预期结果将目录中的每个zip文件提取到一个临时位置,在其中搜索项目,如果找到则打印文件名,然后删除该临时项目
Thanks for the assistance 感谢您的协助
So I think I worked it out using the following 所以我想我用以下方法解决了
using System;
using System.IO;
using Ionic.Zip;
namespace DotNetTest
{
class Program
{
private static string sPath;
private static string sTempLoc = Path.GetTempPath() + Path.GetRandomFileName();
static void Main(string[] args)
{
Console.WriteLine("Enter Path");
sPath = Console.ReadLine();
Console.WriteLine("Looking in " + sPath);
DirectoryInfo di = new DirectoryInfo(sPath + "\\");
FileInfo[] files = di.GetFiles("*.zip");
foreach (var file in files)
{
//Console.WriteLine(file.FullName);
using (ZipFile archive = ZipFile.Read(file.FullName))
{
foreach(ZipEntry e in archive)
{
Console.WriteLine("Extracting " + e.FileName + " to " + sTempLoc);
e.Extract(sTempLoc, ExtractExistingFileAction.OverwriteSilently);
}
}
}
Console.WriteLine("Finsihed! Press any key to exit");
Console.ReadKey();
}
}
}
Here's a method that'll do that. 这是一个可以做到的方法。 Just pass the file's path and the destination directory as arguments. 只需传递文件的路径和目标目录作为参数即可。
private void UnZip(string filePath, string targetPath)
{
if (String.IsNullOrWhiteSpace(filePath) || String.IsNullOrWhiteSpace(targetPath))
{
throw new ArgumentException("Arguments cannot be null or empty.");
}
using (ZipFile zf = ZipFile.Read(filePath))
{
foreach (var entry in zf)
{
entry.Extract(targetPath, ExtractExistingFileAction.OverwriteSilently);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.