Pergunta

I have to list all the information available inside a zip file (FileName, lenght, date created, modified, and so on). Some zip files that I have to analyze come with folders and zip files inside of them as well as other file types.

I was wondering how to read these folders and contents inside of the zip file and also the zip entry inside the zip file without having to unzip the whole thing (if it is even possible)

e.g.:

Collection.zip file1.txt file2.txt reports/ report1.txt report2.txt first-backup.zip second-backup.zip

With the sharp zip library I'm able to read the first layer of files by doing:

foreach (Zip_Library.Zip.ZipEntry entry in new Zip_Library.Zip.ZipFile(fi.FullName))
{
     printObjectProps(entry);
}

And printObjectProps looks kinda like this:

    private static void printObjectProps(Object obj)
    {
        Console.WriteLine("--------------------------------------------");
        Console.WriteLine("ObjectType:" + obj.GetType().ToString());
        Console.WriteLine("--------------------------------------------");
        foreach (PropertyInfo propInfo in obj.GetType().GetProperties())
        {
            Console.WriteLine("PropertyName:" + propInfo.Name);
            Console.WriteLine("PropertyType:" + propInfo.PropertyType);
            Console.WriteLine("PropertyValue:" + propInfo.GetValue(obj, null));
            Console.WriteLine();
        }
        Console.WriteLine("--------------------------------------------");
    }

Any ideas I should consider before I get myself into more trouble??

Foi útil?

Solução

DotNetZip will give you what you want:

http://dotnetzip.codeplex.com/

It's both easy and simple to use. Listing a zipfile directory:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
  foreach (ZipEntry e in zip)
  {
    if (header)
    {
      System.Console.WriteLine("Zipfile: {0}", zip.Name);
      if ((zip.Comment != null) && (zip.Comment != "")) 
        System.Console.WriteLine("Comment: {0}", zip.Comment);
      System.Console.WriteLine("\n{1,-22} {2,8}  {3,5}   {4,8}  {5,3} {0}", "Filename", "Modified", "Size", "Ratio", "Packed", "pw?");
      System.Console.WriteLine(new System.String('-', 72));
      header = false;
    }
      System.Console.WriteLine("{1,-22} {2,8} {3,5:F0}%   {4,8}  {5,3} {0}", e.FileName, e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), e.UncompressedSize, e.CompressionRatio, e.CompressedSize, (e.UsesEncryption) ? "Y" : "N");  
  }
}

Nothing gets uncompressed. It just seeks to the end of the file, then seeks backwards to find the directory/manifest, then reads it.

If you discover that an entry is itself a zip file, and you want to explore that, it's easy to extract that as a temp file and recurse down to explore it, though if I remember right, you can


Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still [available at Codeplex][1]. It looks like the code has migrated to Github:


Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top