Results 1 to 2 of 2

Thread: Simple file compression/decompression routines.

  1. #1

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Lightbulb Simple file compression/decompression routines.

    vbnet Code:
    1. Imports System.IO.Compression
    2. Imports System.IO
    3.  
    4. Public Class FileCompression
    5.     Public Shared Sub CompressFile(ByVal sourceFile As String, ByVal destFile As String)
    6.  
    7.         Dim destStream As New FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.Read)
    8.         Dim srcStream As New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
    9.         Dim gz As New GZipStream(destStream, CompressionMode.Compress)
    10.  
    11.         Dim bytesRead As Integer
    12.         Dim buffer As Byte() = New Byte(10000) {}
    13.  
    14.         bytesRead = srcStream.Read(buffer, 0, buffer.Length)
    15.  
    16.         While bytesRead <> 0
    17.             gz.Write(buffer, 0, bytesRead)
    18.  
    19.             bytesRead = srcStream.Read(buffer, 0, buffer.Length)
    20.         End While
    21.  
    22.         gz.Close()
    23.         destStream.Close()
    24.         srcStream.Close()
    25.     End Sub
    26.  
    27.     Public Shared Sub DecompressFile(ByVal sourceFile As String, ByVal destFile As String)
    28.  
    29.         Dim destStream As New FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.Read)
    30.         Dim srcStream As New FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read)
    31.         Dim gz As New GZipStream(srcStream, CompressionMode.Decompress)
    32.  
    33.         Dim bytesRead As Integer
    34.         Dim buffer As Byte() = New Byte(10000) {}
    35.  
    36.         bytesRead = gz.Read(buffer, 0, buffer.Length)
    37.  
    38.         While bytesRead <> 0
    39.             destStream.Write(buffer, 0, bytesRead)
    40.  
    41.             bytesRead = gz.Read(buffer, 0, buffer.Length)
    42.         End While
    43.  
    44.         gz.Close()
    45.         destStream.Close()
    46.         srcStream.Close()
    47.  
    48.  
    49.     End Sub
    50. End Class

    Usage:-
    vbnet Code:
    1. '
    2.         'Compresses "example.txt" into "example.zip"
    3.         FileCompression.CompressFile("C:\example.txt", "example.zip")
    4.  
    5.         'Decompresses "example.zip" back into "example.txt"
    6.         FileCompression.DecompressFile("C:\example.zip", "C:\example.txt")
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    22

    Re: Simple file compression/decompression routines.

    Many time I could have used that!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width