1

Everything is working fine.. I can Unzip files, from an Zip/Rar .. Archive. The Problem is, how to Unzip a file, thats in a Directory?

To Unzip a File directly I use (SharpZipLib):

FastZip fastZip = new FastZip();
fastZip.ExtractZip(source, targetDirectory, null);

using (var fs = new FileStream(source, FileMode.Open, FileAccess.Read))
    using (var zf = new ZipFile(fs))
    {
        var ze = zf.GetEntry("toc.out");
        if (ze == null)
        {
            throw new ArgumentException("toc.out", "not found in Zip");
        }

        using (var s = zf.GetInputStream(ze))
        {
            // do something with ZipInputStream
        }
     }
}

Or with DotNetZip/ZipDotNet:

using (ZipFile zip = ZipFile.Read(source))
{
    ZipEntry e = zip["toc.out"];
    e.Extract();
}

Thats not working, cause hes searching the file in the root.. And I also wont do something like: DirectoryName/toc.out How can I achieve this`? Isn't there a parameter, where I can include all subfolders - for searching or something similar? :(

oberfreak
  • 1,799
  • 13
  • 20
eMi
  • 5,540
  • 10
  • 60
  • 109
  • possible duplicate of [Using SharpZipLib to unzip specific files?](http://stackoverflow.com/questions/328343/using-sharpziplib-to-unzip-specific-files) – samjudson Nov 29 '11 at 10:06
  • nope. I saw this, there they unzipped files directly, like me, without directories..... – eMi Nov 29 '11 at 10:10

2 Answers2

1

You can write a LINQ expression to find the file in sub folders as shown below

DirectoryInfo dirInfo = new DirectoryInfo(@"C:\");
foreach (var file in dirs.Select(dir => dir.EnumerateFiles().Where(i => i.Name.ToLower() == "wsdl.zip").FirstOrDefault()).Where(file => file != null))
{
    Console.WriteLine(file.ToString());
    Console.WriteLine(file.Length);
}

The above code searches all subfolder under C drive for the file wsdl.zip and prints its name and length to the console.

Hope that helps.

Rajesh
  • 7,766
  • 5
  • 22
  • 35
  • That would be the Idea, but I don't want to first Extract all Files and then to search for the file.. I want to extract the Specific File from the Archive, which is in a Directory.. – eMi Nov 29 '11 at 10:19
  • If your file in the same directory or in a fixed location then you can directly fetch the file. Unless you are sure about the location i guess it would be difficult to search without loading the files. I have made a change to use EnumerateFiles which is more efficient. – Rajesh Nov 29 '11 at 11:05
0

You can check the last part of the Name of the entry. Even if the file is in a subfolder, the Name entry would be something like "Folder/file.ext". An extension method to accomplish this would be like:

public static ZipEntry GetEntryExt(this ZipFile file, string fileName)
{
    foreach (ZipEntry entry in file)
    {
        if (entry.IsFile && entry.Name.EndsWith(Path.DirectorySeparatorChar + fileName))
            return entry;
    }

    return null;
}
tafa
  • 7,146
  • 3
  • 36
  • 40
  • I did it an other way, using FastZip, but this could work too, I will mark the answer as answered, so other know too, that its completed – eMi Nov 29 '11 at 10:48
  • You might want to be careful that you don't return "FooBar.txt" when you search for "Bar.txt", so put a check in that it either matches exactly, or ends with "/Bar.txt". – samjudson Dec 02 '11 at 14:28