.NET 3.5如何使用DotNetZip将ZIP文件解压缩到指定位置

时间:2014-08-29 13:53:17

标签: c# .net

如果安装了.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();
                            }
                        }
                    }
                }
            }
        }

问题在于我们的服务器只运行.NET 3.5,因为我们运行的软件,我需要使用.NET 3.5完成同样的事情

我查看了DotNetZip和DeflateStream类,但我遇到了将其复制到.NET 3.5的麻烦

预期结果将目录中的每个zip文件解压缩到一个临时位置,在其中搜索一个项目,如果找到则打印文件名然后删除临时项目

感谢您的帮助

所以我认为我使用以下

进行了解决
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();
    }
}
}

1 个答案:

答案 0 :(得分:0)

这是一种可以做到这一点的方法。只需将文件的路径和目标目录作为参数传递。

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);
        }
    }
}