C# 和 Zip 文件操作

发布于 2024-09-08 15:59:03 字数 272 浏览 4 评论 0原文

这就是我正在寻找的内容:

我需要打开图像的 zip 文件并迭代其内容。首先,zip 容器文件有子目录,其中一个“IDX”包含我需要的图像。我将 zip 文件内容提取到目录中没有问题。我的 zip 文件可能非常大,以 GB 为单位,因此我希望能够打开该文件并在一次迭代一个图像来处理它们时提取图像。

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

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

谢谢!

Here is what I am looking for:

I need to open a zip file of images and iterate through it's contents. First of all, the zip container file has subdirectories and inside one "IDX" houses the images I need. I have no problem extracting the zip file contents to a directory. 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.

After I am done I just close the zip file. 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? This process will be done on a Windows machine.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

活雷疯 2024-09-15 15:59:03

SharpZipLib 是满足您需求的绝佳工具。

我已经使用它使用流来处理巨型嵌套 zip 文件(即 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 );
}

SharpZipLib is a great tool for your requirements.

I have used it to process giant files within directories within giant nested zip files (meaning ZIP files within ZIP files), using streams. 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. 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). 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 );
}
私藏温柔 2024-09-15 15:59:03

除了 SharpZipLib(工作正常)之外,至少还有两个可行的选择:

There are at least two more viable options besides SharpZipLib (which works fine):

  • DotNetZip on Codeplex

  • Microsoft seems to be investigating integrating ZIP functionality into the System.IO namespace - see this blog post for more info

陌路黄昏 2024-09-15 15:59:03

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

为了让您的员工相信没有 OOTB 方式来打开标准 zip,您应该提及 .NET 还提供了 System.IO.Compression.GZipStream 类,仅对文件流的内容进行压缩(解压缩)。它不会将它们解释为单独的文件、目录等。Jon

Galloway 在“在 .NET 中创建 Zip 存档(没有外部库)",尽管没有选项就像即将推出的 System.IO.Zip 一样干净。

.NET doesn't provide a way to read the contents of a standard ZIP file. The System.IO.Packaging.ZipPackage class can create and read zip files that include a special manifest. ZipPackage can't read files that do not include this file although zip utilities can easily read a .zip created by ZipPackage. If you are the one creating the zips, ZipPackage may be an option. 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.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文