【问题标题】:Can't decompress a file I compressed using BZip2无法解压缩我使用 BZip2 压缩的文件
【发布时间】:2013-03-19 14:15:57
【问题描述】:

我有一个使用 BZip2 压缩文件然后将其保存到新文件的程序。

Public Sub Create(ByVal outPathname As String, ByVal filePath As String)
    Dim msCompressed As New MemoryStream()
    Dim zosCompressed As New BZip2OutputStream(msCompressed)
    Dim fileInput As New FileStream(filePath, FileMode.Open)
    Dim buffer As Byte() = GetStreamAsByteArray(fileInput)
    zosCompressed.Write(buffer, 0, buffer.Length)
    zosCompressed.Flush()
    zosCompressed.Close()
    buffer = msCompressed.ToArray()
    Dim FileOutput As New FileStream(outPathname, FileMode.Create, FileAccess.Write)
    FileOutput.Write(buffer, 0, buffer.LongLength)
    FileOutput.Flush()
    FileOutput.Close()
End Sub

但我似乎无法弄清楚为什么我无法解压缩它创建的文件。有人能告诉我这里的减压器有什么问题吗? (它与压缩器在一个单独的程序中。)

Public Sub Extract(ByVal archiveFilenameIn As String, ByVal outFolder As String)
    Dim fileInput As New FileStream(archiveFilenameIn, FileMode.Open)
    Dim msUncompressed As New MemoryStream(GetStreamAsByteArray(fileInput))
    Dim zosDecompressed As New BZip2InputStream(msUncompressed)
    Dim buffer As Byte() = GetStreamAsByteArray(fileInput)
    zosDecompressed.Read(buffer, 0, buffer.Length())
    buffer = msUncompressed.ToArray()
    Dim FileOutput As New FileStream(outFolder & "\temporary.bmp", FileMode.Create, FileAccess.Write)
    FileOutput.Write(buffer, 0, buffer.LongLength)
    FileOutput.Flush()
    FileOutput.Close()
    zosDecompressed.Close()
    msUncompressed.Close()
    fileInput.Close()
End Sub

编辑:我在这里还使用了一个将流转换为字节数组的函数:

Private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
    Dim streamLength As Integer = Convert.ToInt32(stream.Length)
    Dim fileData As Byte() = New Byte(streamLength) {}

    ' Read the file into a byte array
    stream.Read(fileData, 0, streamLength)
    stream.Close()

    Return fileData
End Function

帮助?

【问题讨论】:

    标签: vb.net compression bytearray sharpziplib


    【解决方案1】:

    在使用写入数据之前尝试完成 BZip2OutputStream。这对我很有帮助。

    zosCompressed.Finalize();
    buffer = msCompressed.ToArray();
    

    BZip2OutputStream usage example.

    【讨论】:

    • 它的用处是有限的,因为调用Finalize() 会关闭底层内存流,完全违背了压缩内存流的目的。
    猜你喜欢
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2012-12-12
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多