Compressie

Status
Niet open voor verdere reacties.

famlam

Gebruiker
Lid geworden
15 okt 2008
Berichten
416
Ik gebruik nu een andere code, die weliswaar ook zijn probleempjes heeft, maar niet deze problemen...

Code:
Imports System.IO
Imports System.IO.Compression

Public Class Form1
    Private bronZIP As String = "C:\testzip.zip"
    Private bronEXE As String = "C:\testexe.exe"
    Public Sub CompressFile(ByVal sourceFile As String, ByVal destinationFile As String)

        If Not File.Exists(sourceFile) Then Throw New FileNotFoundException

        Dim buffer As Byte() = Nothing
        Dim sourceStream As FileStream = Nothing
        Dim destinationStream As FileStream = Nothing
        Dim compressedStream As DeflateStream = Nothing

        Try
            ' Read the bytes from the source file into a byte array
            sourceStream = New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)

            ' Read the source stream values into the buffer
            buffer = New Byte(sourceStream.Length) {}
            Dim checkCounter As Integer = sourceStream.Read(buffer, 0, buffer.Length)

            ' Open the FileStream to write to
            destinationStream = New FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)

            ' Create a compression stream pointing to the destiantion stream
            compressedStream = New DeflateStream(destinationStream, CompressionMode.Compress, True)

            'Now write the compressed data to the destination file
            compressedStream.Write(buffer, 0, buffer.Length)

        Catch ex As ApplicationException
            MessageBox.Show(ex.Message, "Mislukt!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not (sourceStream Is Nothing) Then sourceStream.Close()
            If Not (compressedStream Is Nothing) Then compressedStream.Close()
            If Not (destinationStream Is Nothing) Then destinationStream.Close()
        End Try
    End Sub

    Public Sub DecompressFile(ByVal sourceFile As String, ByVal destinationFile As String)
        If Not File.Exists(sourceFile) Then Throw New FileNotFoundException

        Dim sourceStream As FileStream = Nothing
        Dim destinationStream As FileStream = Nothing
        Dim decompressedStream As DeflateStream = Nothing
        Dim quartetBuffer As Byte() = Nothing

        Try
            ' Read in the compressed source stream
            sourceStream = New FileStream(sourceFile, FileMode.Open)

            ' Create a compression stream pointing to the destiantion stream
            decompressedStream = New DeflateStream(sourceStream, CompressionMode.Decompress, True)

            ' Read the footer to determine the length of the destiantion file
            quartetBuffer = New Byte(4) {}
            Dim position As Integer = CType(sourceStream.Length, Integer) - 4
            sourceStream.Position = position
            sourceStream.Read(quartetBuffer, 0, 4)
            sourceStream.Position = 0
            Dim checkLength As Integer = BitConverter.ToInt32(quartetBuffer, 0)

            Dim buffer(checkLength + 100) As Byte
            Dim offset As Integer = 0
            Dim total As Integer = 0

            ' Read the compressed data into the buffer
            While True
                Dim bytesRead As Integer = decompressedStream.Read(buffer, offset, 100)
                If bytesRead = 0 Then
                    Exit While
                End If
                offset += bytesRead
                total += bytesRead
            End While

            ' Now write everything to the destination file
            destinationStream = New FileStream(destinationFile, FileMode.Create)
            destinationStream.Write(buffer, 0, total)

            ' and flush everyhting to clean out the buffer
            destinationStream.Flush()

        Catch ex As ApplicationException
            MessageBox.Show(ex.Message, "Mislukt!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If Not (sourceStream Is Nothing) Then sourceStream.Close()
            If Not (decompressedStream Is Nothing) Then decompressedStream.Close()
            If Not (destinationStream Is Nothing) Then destinationStream.Close()
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CompressFile(bronEXE, bronZIP)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DecompressFile(bronZIP, bronEXE.ToString + "2")
    End Sub
End Class

Deze code (die ik bijna letterlijk van het internet heb geplukt) zou bestanden moeten kunnen comprimeren en weer decomprimeren.
2 kleine probleempjes:
  1. Het rendement van deze compressie ligt op -150%, d.w.z. dat de bestandsgrootte van bijvoorbeeld 100kB naar 150kB gaat
  2. Het (gecomprimeerde) resultaat kan niet geopend worden in enig ander (un)zipprogramma.

Overigens: exe --> zip --> exe levert het goede bestand op... de methode werkt dus wel

Heeft er iemand een idee over hoe ik deze twee 'nog net niet verwaarloosbare' (sarcastisch) probleempjes op kan lossen?
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan