在 vb.net 中解压缩文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18202765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:37:47  来源:igfitidea点击:

Unzipping a File in vb.net

vb.netgzipstream

提问by Spartin503

I have an embedded resource in my .exe that is a zip file, I would like to move it out of the resources and unzip it to a specific folder.

我的 .exe 中有一个嵌入的资源,它是一个 zip 文件,我想将它移出资源并将其解压缩到特定文件夹。

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btn_Install.Click
    Dim Dir_File As String = "C:\FTB"
    Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"

    System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)

    Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)

End Sub

But I have no way of extracting the the .zip file to a directory. So all I need now is a way to actual extract the .zip.

但是我无法将 .zip 文件解压缩到目录中。所以我现在需要的是一种实际提取 .zip 的方法。

I tried this:

我试过这个:

Dim directorySelected As DirectoryInfo = New DirectoryInfo(Dir_Temp)
    For Each fileToDecompress As FileInfo In directorySelected.GetFiles("Unleashed.zip")
        Using OriginalFileStream As FileStream = fileToDecompress.OpenRead()
            Using decompressedFileStream As FileStream = File.Create(Dir_File & "\Unleashed")
                Using decompressionStream As GZipStream = New GZipStream(OriginalFileStream, CompressionMode.Decompress)
                    decompressionStream.CopyTo(decompressedFileStream)
                End Using
            End Using
        End Using
    Next

But all that did was give me an error about a magic number. Any help is very much appreciated.

但所做的只是给我一个关于幻数的错误。很感谢任何形式的帮助。

回答by Heslacher

Your problem is allready discussed here: https://stackoverflow.com/a/11273427/2655508

您的问题已经在这里讨论过:https: //stackoverflow.com/a/11273427/2655508

The GZipStream class is not suitable to read compressed archives in the .gz or .zip format. It only knows how to de/compress data, it doesn't know anything about the archive file structure. Which can contain multiple files, note how the class doesn't have any way to select the specific file in the archive you want to decompress.

GZipStream 类不适合读取 .gz 或 .zip 格式的压缩档案。它只知道如何解压缩数据,它对存档文件结构一无所知。其中可以包含多个文件,请注意该类如何无法选择要解压缩的存档中的特定文件。

The solution is to use NET 4.5 which contains inside the System.IO.Compression namespace the needed classes with their methods for creating, manipulating and extracting items in/out of a zip file.

解决方案是使用 NET 4.5,它在 System.IO.Compression 命名空间内包含所需的类及其用于在 zip 文件中创建、操作和提取项目的方法。

See e.g. System.IO.Compression.ZipFileExtensions.ExtractToDirectory()

参见例如System.IO.Compression.ZipFileExtensions.ExtractToDirectory()

回答by Tarik

Gzip is different from zip. The file format is identified by the 2 to 4 first bytes of the file: Zip: 50 4b 03 04 Gzip: 1f 8b Since you are trying to decompress a zip file with gzip you get an error message essencially telling you that your file is not a gzip file.

Gzip 不同于 zip。文件格式由文件的前 2 到 4 个字节标识: Zip: 50 4b 03 04 Gzip: 1f 8b 由于您尝试使用 gzip 解压缩 zip 文件,因此您会收到一条错误消息,基本上告诉您您的文件不是一个 gzip 文件。

See Unzip files programmatically in .netfor similar question. For magic number see: http://en.m.wikipedia.org/wiki/Magic_number_(programming)

有关类似问题,请参阅 在 .net 中编程方式解压缩文件。有关幻数,请参阅:http: //en.m.wikipedia.org/wiki/Magic_number_(programming)

回答by Irvin Dominin

If you can use the NET 4.5 it ships inside the System.IO.Compressionnamespace and all the stuff needed for zip file manipulation.

如果您可以使用 NET 4.5,它会在System.IO.Compression命名空间以及 zip 文件操作所需的所有内容中提供。

For zip manipulation you can consider using a third party library like sharpziplibI use it with success in many projects.

对于 zip 操作,您可以考虑使用第三方库,就像sharpziplib我在许多项目中成功使用它一样。

Take a look to these samples: https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples

看看这些示例:https: //github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples