GZip 流错误

发布于 2024-10-02 08:12:06 字数 2102 浏览 3 评论 0原文

我使用下面的代码并收到此错误 InvalidDataExcption: GZip 标头中的幻数不正确。确保您传递的是 GZip 流。 我正在尝试提取 Microsoft word .docx 文件。 (代码荧光笔对我的评论做了奇怪的事情)

Imports System.IO
Imports System.IO.Compression
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim path As String = "C:\Users\Gio\Documents\Test.zip"
    'path.Split("\")(path.Split("\").Length - 1)
    DecompressFile(path, "test", "C:\Users\Gio\Documents")

End Sub

''' <summary>
'''Function to decompress a file using the GZipStream Class
''' </summary>
''' <param name="inputFileName">File that we want to decompress</param>
''' <param name="destFileName">Name we want the decompressed file to be</param>
''' <param name="destDirectory">Directory to save the file to</param>
''' <returns>True/False</returns>
''' <remarks></remarks>
Public Function DecompressFile(ByRef inputFileName As String, ByRef destFileName As String, ByRef destDirectory As String) As Boolean
    'Try
    'Create a MemoryStream from the file bytes
    Dim stream As New MemoryStream(File.ReadAllBytes(inputFileName))

    'Create a new GZipStream from the MemoryStream
    Dim gZip As New GZipStream(stream, CompressionMode.Decompress)

    'Byte array to hold bytes
    Dim buffer(3) As Byte

    'Read the stream
    stream.Position = stream.Length - 5
    stream.Read(buffer, 0, 4)

    'Calculate the size of the decompressed bytes
    Dim size As Integer = BitConverter.ToInt32(buffer, 0)

    'Start at the beginning of the stream
    stream.Position = 0

    Dim decompressed(size - 1) As Byte

    'Read decompressed bytes into byte array
    gZip.Read(decompressed, 0, size)

    'Close & clean up
    gZip.Dispose()
    stream.Dispose()

    'Write the final file
    File.WriteAllBytes(destDirectory & "\" & destFileName, decompressed)

    Return True
    'Catch ex As Exception
    '    MessageBox.Show(ex.ToString())
    '   Return False
    'End Try
End Function

End Class

I use the code below and get this error InvalidDataExcption: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.
I am trying to extract a Microsoft word .docx file.
(the code highlighter is doing weird stuff to my comments)

Imports System.IO
Imports System.IO.Compression
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim path As String = "C:\Users\Gio\Documents\Test.zip"
    'path.Split("\")(path.Split("\").Length - 1)
    DecompressFile(path, "test", "C:\Users\Gio\Documents")

End Sub

''' <summary>
'''Function to decompress a file using the GZipStream Class
''' </summary>
''' <param name="inputFileName">File that we want to decompress</param>
''' <param name="destFileName">Name we want the decompressed file to be</param>
''' <param name="destDirectory">Directory to save the file to</param>
''' <returns>True/False</returns>
''' <remarks></remarks>
Public Function DecompressFile(ByRef inputFileName As String, ByRef destFileName As String, ByRef destDirectory As String) As Boolean
    'Try
    'Create a MemoryStream from the file bytes
    Dim stream As New MemoryStream(File.ReadAllBytes(inputFileName))

    'Create a new GZipStream from the MemoryStream
    Dim gZip As New GZipStream(stream, CompressionMode.Decompress)

    'Byte array to hold bytes
    Dim buffer(3) As Byte

    'Read the stream
    stream.Position = stream.Length - 5
    stream.Read(buffer, 0, 4)

    'Calculate the size of the decompressed bytes
    Dim size As Integer = BitConverter.ToInt32(buffer, 0)

    'Start at the beginning of the stream
    stream.Position = 0

    Dim decompressed(size - 1) As Byte

    'Read decompressed bytes into byte array
    gZip.Read(decompressed, 0, size)

    'Close & clean up
    gZip.Dispose()
    stream.Dispose()

    'Write the final file
    File.WriteAllBytes(destDirectory & "\" & destFileName, decompressed)

    Return True
    'Catch ex As Exception
    '    MessageBox.Show(ex.ToString())
    '   Return False
    'End Try
End Function

End Class

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

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

发布评论

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

评论(1

孤独岁月 2024-10-09 08:12:06

.docx 文件以 pkzip 格式保存。这与 gzip 格式完全不同。

SharpZipLib 是您可以用来读取这些文件的一个选项,尽管还有许多其他选项也存在。

.docx files are saved in pkzip format. That's completely different than gzip format.

SharpZipLib is one option you can use to read these files, although many others exist also.

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