简体   繁体   English

C#和Zip文件操作

[英]C# and Zip file manipulation

Here is what I am looking for: 这是我正在寻找的:

I need to open a zip file of images and iterate through it's contents. 我需要打开一个zip文件的图像并迭代它的内容。 First of all, the zip container file has subdirectories and inside one "IDX" houses the images I need. 首先,zip容器文件有子目录,在一个“IDX”里面有我需要的图像。 I have no problem extracting the zip file contents to a directory. 我将zip文件内容解压缩到目录没有问题。 My zip files can be incredibly huge, as in GBs huge, and so I am hoping to be able to open the file and pull out the images as I iterate through them one at a time to process them. 我的zip文件非常庞大,就像在GB中一样巨大,所以我希望能够打开文件并拉出图像,因为我一次一个地迭代它们来处理它们。

After I am done I just close the zip file. 完成后我只关闭zip文件。 These images are actually being housed in a database. 这些图像实际上存放在数据库中。

Does anyone have any idea how to do this with, hopefully, free tools or built-in api's? 有没有人知道如何使用,希望,免费工具或内置api? This process will be done on a Windows machine. 此过程将在Windows计算机上完成。

Thanks! 谢谢!

SharpZipLib is a great tool for your requirements. SharpZipLib是满足您要求的绝佳工具。

I have used it to process giant files within directories within giant nested zip files (meaning ZIP files within ZIP files), using streams. 我用它来处理巨型嵌套zip文件(即ZIP文件中的ZIP文件)内的目录中的巨型文件,使用流。 I was able to open a zip stream on top of a zip stream so that I could investigate the contents of the inner zip without having to extract the entire parent. 我能够在压缩流的顶部打开一个拉链流这样我就可以调查内部zip的内容而无需提取整个父级。 You can then use a stream to peek at the content files, which may help you determine whether you want to extract it or not. 然后,您可以使用流来查看内容文件,这可以帮助您确定是否要提取它。 It's open-source. 它是开源的。

EDIT: Directory handling in the library is not ideal. 编辑:库中的目录处理并不理想。 As I recall, it contains separate entries for some directories, while others are implied by the paths of the file entries. 我记得,它包含一些目录的单独条目,而其他目录则隐含在文件条目的路径中。

Here's an extract of the code I used to collect the actual file and folder names at a certain level (_startPath). 这是我用于收集特定级别(_startPath)的实际文件和文件夹名称的代码的摘录。 Let me know if you're interested in the whole wrapper class. 如果您对整个包装类感兴趣,请告诉我。

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

There are at least two more viable options besides SharpZipLib (which works fine): 除了SharpZipLib之外,还有至少两个更可行的选项(工作正常):

.NET doesn't provide a way to read the contents of a standard ZIP file. .NET不提供读取标准ZIP文件内容的方法。 The System.IO.Packaging.ZipPackage class can create and read zip files that include a special manifest. System.IO.Packaging.ZipPackage类可以创建和读取包含特殊清单的zip文件。 ZipPackage can't read files that do not include this file although zip utilities can easily read a .zip created by ZipPackage. 尽管zip实用程序可以轻松读取ZipPackage创建的.zip,但ZipPackage无法读取不包含此文件的文件。 If you are the one creating the zips, ZipPackage may be an option. 如果您是创建拉链的人,可以选择ZipPackage。 The classes used to perform the actual compression and creation of the .zip file are internal to System.IO.Packaging so you can't use it directly. 用于执行.zip文件的实际压缩和创建的类是System.IO.Packaging的内部,因此您无法直接使用它。

To convince your people that there is no OOTB way to open standard zips, you should mention that .NET also provides the System.IO.Compression.GZipStream class which only (de)compresses the contents of a file stream. 为了说服你的员工没有OOTB方式来打开标准zip,你应该提到.NET还提供了System.IO.Compression.GZipStream类,它只(压缩)压缩文件流的内容。 It does not interpret them to separate files, directories etc. 它不会将它们解释为单独的文件,目录等。

Jon Galloway covered all the options a while back in " Creating Zip archives in .NET (without an external library) ", although no option as clean as the upcoming System.IO.Zip. Jon Galloway在“ 在.NET中创建Zip存档(没有外部库) ”中提到了所有选项,尽管没有像即将发布的System.IO.Zip那样干净的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM