gpt4 book ai didi

C# 和 Zip 文件操作

转载 作者:行者123 更新时间:2023-11-30 19:33:41 25 4
gpt4 key购买 nike

这是我要找的:

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

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

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

谢谢!

最佳答案

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

关于C# 和 Zip 文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3205208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com