Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中列出.zip文件夹的内容?_C#_List_Directory_Zip - Fatal编程技术网

如何在c#中列出.zip文件夹的内容?

如何在c#中列出.zip文件夹的内容?,c#,list,directory,zip,C#,List,Directory,Zip,如何在C#中列出压缩文件夹的内容?例如,如何知道一个压缩文件夹中包含了多少项,以及它们的名称?请登录 -Zip文件在.NET语言中的操作 DotNetZip是一个小型、易于使用的类库,用于处理.zip文件。它可以使用VB.NET、C#或任何.NET语言编写的.NET应用程序轻松创建、读取和更新zip文件 读取zip的示例代码: using (var zip = ZipFile.Read(PathToZipFolder)) { int totalEntries = zip.Entries.

如何在C#中列出压缩文件夹的内容?例如,如何知道一个压缩文件夹中包含了多少项,以及它们的名称?

请登录

-Zip文件在.NET语言中的操作

DotNetZip是一个小型、易于使用的类库,用于处理.zip文件。它可以使用VB.NET、C#或任何.NET语言编写的.NET应用程序轻松创建、读取和更新zip文件

读取zip的示例代码:

using (var zip = ZipFile.Read(PathToZipFolder))
{
    int totalEntries = zip.Entries.Count; 
    foreach (ZipEntry e in zip.Entries)
    {
        e.FileName ...
        e.CompressedSize ...
        e.LastModified...
    }
}

如果您使用的是.Net Framework 3.0或更高版本,请查看。这将消除您对外部库的依赖性


具体查看。

我在这里相对较新,所以可能我不了解发生了什么事。:-) 目前有4个答案,其中两个最佳答案被否决。(Pearcewg和cxfx)Pearcewg指出的这篇文章很重要,因为它澄清了SharpZipLib的一些许可问题。 我们最近评估了几个.Net压缩库,发现DotNetZip是目前最好的替代品

非常简短的摘要:

  • System.IO.Packaging的速度明显慢于DotNetZip

  • SharpZipLib是GPL——见文章

因此,首先,我投票支持这两个答案


Kim.

最好的方法是使用.NET内置的J#zip功能,如MSDN所示:。在此链接中,有一个完整的应用程序读取和写入zip文件的工作示例。对于列出zip文件内容的具体示例(本例中为Silverlight.xap应用程序包),代码可能如下所示:


ZipFile package = new ZipFile(packagePath);
java.util.Enumeration entries = package.entries();
//We have to use Java enumerators because we
//use java.util.zip for reading the .zip files
while ( entries.hasMoreElements() )
{
    ZipEntry entry = (ZipEntry) entries.nextElement();

    if (!entry.isDirectory())
    {
        string name = entry.getName();
        Console.WriteLine("File: " + name + ", size: " + entry.getSize() + ", compressed size: " + entry.getCompressedSize());
    }
    else
    {
        // Handle directories...
    }                        
}
             string packageRelationshipType = @"http://schemas.microsoft.com/opc/2006/sample/document";
            string resourceRelationshipType = @"http://schemas.microsoft.com/opc/2006/sample/required-resource";
            // Open the Package.
            // ('using' statement insures that 'package' is
            //  closed and disposed when it goes out of scope.)
            foreach (string packagePath in downloadedFiles)
            {
                Logger.Warning("Analyzing " + packagePath);
                using (Package package = Package.Open(packagePath, FileMode.Open, FileAccess.Read))
                {
                    Logger.OutPut("package opened");
                    PackagePart documentPart = null;
                    PackagePart resourcePart = null;

                    // Get the Package Relationships and look for
                    //   the Document part based on the RelationshipType
                    Uri uriDocumentTarget = null;
                    foreach (PackageRelationship relationship in
                        package.GetRelationshipsByType(packageRelationshipType))
                    {
                        // Resolve the Relationship Target Uri
                        //   so the Document Part can be retrieved.
                        uriDocumentTarget = PackUriHelper.ResolvePartUri(
                            new Uri("/", UriKind.Relative), relationship.TargetUri);

                        // Open the Document Part, write the contents to a file.
                        documentPart = package.GetPart(uriDocumentTarget);
                        //ExtractPart(documentPart, targetDirectory);
                        string stringPart = documentPart.Uri.ToString().TrimStart('/');
                        Logger.OutPut("  Got: " + stringPart);
                    }

                    // Get the Document part's Relationships,
                    //   and look for required resources.
                    Uri uriResourceTarget = null;
                    foreach (PackageRelationship relationship in
                        documentPart.GetRelationshipsByType(
                                                resourceRelationshipType))
                    {
                        // Resolve the Relationship Target Uri
                        //   so the Resource Part can be retrieved.
                        uriResourceTarget = PackUriHelper.ResolvePartUri(
                            documentPart.Uri, relationship.TargetUri);

                        // Open the Resource Part and write the contents to a file.
                        resourcePart = package.GetPart(uriResourceTarget);

                        //ExtractPart(resourcePart, targetDirectory);
                        string stringPart = resourcePart.Uri.ToString().TrimStart('/');
                        Logger.OutPut("  Got: " + stringPart);
                    }

                }
            }
艾德斯曼有一个正确的指针,但有。具体来说,您可能会发现打开zip文件时出现问题,但如果您只想创建包,这是一个有效的解决方案。ZipPackage实现抽象包类并允许对zip文件进行操作。在MSDN中有一个如何执行此操作的示例:。代码大致如下所示:


ZipFile package = new ZipFile(packagePath);
java.util.Enumeration entries = package.entries();
//We have to use Java enumerators because we
//use java.util.zip for reading the .zip files
while ( entries.hasMoreElements() )
{
    ZipEntry entry = (ZipEntry) entries.nextElement();

    if (!entry.isDirectory())
    {
        string name = entry.getName();
        Console.WriteLine("File: " + name + ", size: " + entry.getSize() + ", compressed size: " + entry.getCompressedSize());
    }
    else
    {
        // Handle directories...
    }                        
}
             string packageRelationshipType = @"http://schemas.microsoft.com/opc/2006/sample/document";
            string resourceRelationshipType = @"http://schemas.microsoft.com/opc/2006/sample/required-resource";
            // Open the Package.
            // ('using' statement insures that 'package' is
            //  closed and disposed when it goes out of scope.)
            foreach (string packagePath in downloadedFiles)
            {
                Logger.Warning("Analyzing " + packagePath);
                using (Package package = Package.Open(packagePath, FileMode.Open, FileAccess.Read))
                {
                    Logger.OutPut("package opened");
                    PackagePart documentPart = null;
                    PackagePart resourcePart = null;

                    // Get the Package Relationships and look for
                    //   the Document part based on the RelationshipType
                    Uri uriDocumentTarget = null;
                    foreach (PackageRelationship relationship in
                        package.GetRelationshipsByType(packageRelationshipType))
                    {
                        // Resolve the Relationship Target Uri
                        //   so the Document Part can be retrieved.
                        uriDocumentTarget = PackUriHelper.ResolvePartUri(
                            new Uri("/", UriKind.Relative), relationship.TargetUri);

                        // Open the Document Part, write the contents to a file.
                        documentPart = package.GetPart(uriDocumentTarget);
                        //ExtractPart(documentPart, targetDirectory);
                        string stringPart = documentPart.Uri.ToString().TrimStart('/');
                        Logger.OutPut("  Got: " + stringPart);
                    }

                    // Get the Document part's Relationships,
                    //   and look for required resources.
                    Uri uriResourceTarget = null;
                    foreach (PackageRelationship relationship in
                        documentPart.GetRelationshipsByType(
                                                resourceRelationshipType))
                    {
                        // Resolve the Relationship Target Uri
                        //   so the Resource Part can be retrieved.
                        uriResourceTarget = PackUriHelper.ResolvePartUri(
                            documentPart.Uri, relationship.TargetUri);

                        // Open the Resource Part and write the contents to a file.
                        resourcePart = package.GetPart(uriResourceTarget);

                        //ExtractPart(resourcePart, targetDirectory);
                        string stringPart = resourcePart.Uri.ToString().TrimStart('/');
                        Logger.OutPut("  Got: " + stringPart);
                    }

                }
            }
最好的方法似乎是使用J#,如MSDN所示:


本文中有指向更多具有不同许可证的c#.zip库的指针,如SharpNetZip和DotNetZip:。由于许可证要求,它们可能不合适。

Ick-使用J#runtime的代码太可怕了!我也不认为这是最好的办法——J#现在已经失去了支持。如果您只需要ZIP支持,那么这是一个巨大的运行时

这个怎么样?它使用(免费,MS公共许可证)


如果您和我一样,不想使用外部组件,下面是我昨晚使用.NET的ZipPackage类开发的一些代码

var zipFilePath = "c:\\myfile.zip";
var tempFolderPath = "c:\\unzipped";

using (Package package = ZipPackage.Open(zipFilePath, FileMode.Open, FileAccess.Read))
{
    foreach (PackagePart part in package.GetParts())
    {
        var target = Path.GetFullPath(Path.Combine(tempFolderPath, part.Uri.OriginalString.TrimStart('/')));
        var targetDir = target.Remove(target.LastIndexOf('\\'));

        if (!Directory.Exists(targetDir))
            Directory.CreateDirectory(targetDir);

        using (Stream source = part.GetStream(FileMode.Open, FileAccess.Read))
        {
            source.CopyTo(File.OpenWrite(target));
        }
    }
}
注意事项:

  • ZIP存档文件的根目录中必须有[Content\u Types].xml文件。这对我的需求来说不是问题,因为我将控制通过此代码提取的任何ZIP文件的压缩。有关[Content\u Types].xml文件的更多信息,请参阅:本文图13下面有一个示例文件

  • 此代码使用.NET 4.0中的Stream.CopyTo方法


.NET 4.5或更新版本最终具有内置功能,可以使用assembly System.IO.Compression中的
System.IO.Compression.ZipArchive
类()处理通用zip文件。不需要任何第三方库

string zipPath = @"c:\example\start.zip";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        Console.WriteLine(entry.FullName);
    }
} 

GPL本身专门解决了程序中非GPLed代码“派生”部分的问题,以及它们独立获得许可的权利。我看不出SharpZipLib成为GPL是一个多么重要的问题。GC,你是说如果一个团体或组织的GPL有问题,那么这个团体是错的?或者是你根本不可能理解任何人怎么会对GPL有意见?这并不难相信。媒体上充斥着昂贵的法律测试的报道。是的,GPL是病毒性的。如果他正在制作商业的封闭源代码软件,并且他将自己的代码链接到一个GPLed库,那么他必须在一个GPL兼容的许可证下发布他的源代码。是的,SharpZibLib是GPL这一事实是一个重要的问题;关于许可证的讨论(在任何情况下都很重要,但在这里不是唯一的,与问题无关);你如何投票的记录;还有一些图书馆的建议。它不是什么:回答了“如何在c#?中列出.zip文件夹的内容”的问题。它消除了对外部库的依赖,但ZipPackage的可用性确实不是很好。一切都是交换
ZipPackage
主要用于处理内部为zip文件的Microsoft相关文件格式,例如:
docx
xlsx
XPS
nupkg
。从通用zip文件的角度来看,这些文件的不同之处在于归档文件根目录中存在
Content\u Type.xml
文件。如果您不能使用.NET 4.5,但可以使用.NET 3.0,
ZipPackage
可以非常完美,因为您可以生成和使用这些文件。对于.NET 4.5或更高版本,虽然应该使用
zipparchive
类来实现一般的zip目的,请参见下面的答案否,这不是最好的。J#运行时基于一个旧的Java库,zip文件有bug,并且它已经不受支持了。如果是.NET 4.5或更新版本,您就不再需要第三方库了!请看下面我的答案!MDSN杂志的月份和年份?工作良好,开箱即用,无需第三方组件。确保添加引用
System.IO.Compression.dll
System.IO.Compression.FileSystem.dll