代码之家  ›  专栏  ›  技术社区  ›  Tacoman667

C和zip文件操作

  •  2
  • Tacoman667  · 技术社区  · 16 年前

    我要找的是:

    我需要打开一个图像的zip文件,并迭代它的内容。首先,zip容器文件有子目录,在一个“idx”中包含我需要的图像。我可以将zip文件内容解压缩到目录中。我的zip文件可能非常大,就像gbs的文件一样,所以我希望能够打开该文件并在每次迭代一个图像以处理它们时将其拉出。

    完成后,我只需关闭zip文件。这些图像实际上被保存在数据库中。

    有人知道如何使用免费工具或内置API来实现这一点吗?此过程将在Windows计算机上完成。

    谢谢!

    3 回复  |  直到 16 年前
        1
  •  6
  •   harpo Binary Worrier    16 年前

    SharpZipLib 是满足您需求的好工具。

    我使用它来处理巨型嵌套zip文件(即zip文件中的zip文件)目录中的巨型文件,使用流。我打开了一个压缩流 顶上 一个zip流,这样我就可以在不提取整个父级的情况下调查内部zip的内容。然后可以使用流查看内容文件,这可能有助于确定是否要提取内容文件。它是开源的。

    编辑: 库中的目录处理不理想。我记得,它为一些目录包含单独的条目,而其他的则由文件条目的路径隐含。

    下面是我用来在特定级别收集实际文件和文件夹名称的代码的摘录(StartPath)。如果您对整个包装类感兴趣,请告诉我。

    // _zipFile = your ZipFile instance
    List<string> _folderNames = new List<string>();
    List<string> _fileNames = nwe List<string>();
    string _startPath = "";
    const string PATH_SEPARATOR = "/";
    
    foreach ( ZipEntry entry in _zipFile )
    {
        string name = entry.Name;
    
        if ( _startPath != "" )
        {
            if ( name.StartsWith( _startPath + PATH_SEPARATOR ) )
                name = name.Substring( _startPath.Length + 1 );
            else
                continue;
        }
    
        // Ignore items below this folder
        if ( name.IndexOf( PATH_SEPARATOR ) != name.LastIndexOf( PATH_SEPARATOR ) )
            continue;
    
        string thisPath = null;
        string thisFile = null;
    
        if ( entry.IsDirectory ) {
            thisPath = name.TrimEnd( PATH_SEPARATOR.ToCharArray() );
        }
        else if ( entry.IsFile )
        {
            if ( name.Contains( PATH_SEPARATOR ) )
                thisPath = name.Substring( 0, name.IndexOf( PATH_SEPARATOR ) );
            else
                thisFile = name;
        }
    
        if ( !string.IsNullOrEmpty( thisPath ) && !_folderNames.Contains( thisPath ) )
            _folderNames.Add( thisPath );
    
        if ( !string.IsNullOrEmpty( thisFile ) && !_fileNames.Contains( thisFile ) )
            _fileNames.Add( thisFile );
    }
    
        2
  •  3
  •   marc_s MisterSmith    16 年前

    除了sharpziplib(很好用),至少还有两个可行的选项:

        3
  •  0
  •   Panagiotis Kanavos    16 年前

    .NET不提供读取标准zip文件内容的方法。这个 System.IO.Packaging.ZipPackage 类可以创建和读取包含特殊清单的zip文件。ZipPackage无法读取不包含此文件的文件,尽管Zip实用程序可以轻松读取由ZipPackage创建的.zip。如果您是创建Zips的人,则ZipPackage可能是一个选项。用于执行.zip文件的实际压缩和创建的类是System.IO.Packaging的内部类,因此您不能直接使用它。

    为了让您的人相信没有OOTB方法来打开标准的ZIP,您应该提到.NET还提供了 System.IO.Compression.GZipStream 类,它只压缩文件流的内容。它不会将它们解释为单独的文件、目录等。

    乔恩·盖洛韦在一段时间内涵盖了所有的选择。 Creating Zip archives in .NET (without an external library) “,尽管没有比即将推出的system.io.zip更干净的选项。

    推荐文章