首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GZipStream错误

GZipStream错误
EN

Stack Overflow用户
提问于 2010-11-11 12:59:04
回答 1查看 792关注 0票数 0

我使用下面的代码,并得到此错误InvalidDataExcption: The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.我试图提取一个Microsoft word的.docx文件。(代码高亮工具对我的评论做了一些奇怪的事情)

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-11 13:22:57

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4151473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档