Storing zip file in SQL Server database

Feb 24, 2009 at 7:48 PM

I successfully used Ionic.Zip to create a zipfile and add items to it.  Now I need to use Ionic.ZLib to read a zip stream into a byte array so I can store the .zip file as a binary in the SQL Server database.  When I declare Ionic.ZLib.ZLibStream ,

 

Dim

 

sZipFile As New Ionic.Zlib.ZlibStream

 

 

it says that ZLibStream is ambiguous in the namespace ZLib, and I can't get past that error to write any further code.  I am using VB.NET.  I referenced both Ionic.Lib and Ionic.ZLib in my project.  What am I doing wrong?  Thank you for help.  This is a great library.

Krystal

Coordinator
Feb 24, 2009 at 8:59 PM
Edited Feb 24, 2009 at 9:01 PM
Hey Krystal, thanks for using the forum. 
 
Here's some code I worked up to demonstrate the use of the ZlibStream.
' zlib1.vb
'
' Demonstrate the use of the ZlibStream
'
' Author: Dinoch
' Created: Tue Feb 24 13:21:36 2009
'
' last saved: 
' Time-stamp: <2009-February-24 13:46:38>
' ------------------------------------------------------------------
'
' Copyright (c) 2009 by Dino Chiesa
' All rights reserved!
' 
' ------------------------------------------------------------------


Imports System
Imports Ionic.Zlib
Imports System.IO

Namespace Ionic.Test.Krystal 


Public Class zlib1

   ' Fields
    Private _CompressedFile As String
    Private _Crc1 As Integer
    Private _Crc2 As Integer
    Private _DecompressedFile As String
    Private _FileToCompress As String
    Private _WantZlib As Boolean
    Private Const WORKING_BUFFER_SIZE As Integer = 4000

    'ctors
    Public Sub New()
        Me._WantZlib = False
    End Sub

    Public Sub New(ByVal args As String())
        Me._WantZlib = False
        Dim i As Integer
        For i = 0 To args.Length - 1
            If (Not args(i) Is Nothing) Then
              If (args(i) = "-zlib") Then
                Me._WantZlib = True
                Continue For
              Else
		If (Not Me._FileToCompress Is Nothing) Then
		    zlib1.Usage
		    Exit For
		End If
                Me._FileToCompress = args(i)
              End If
            End If
        Next i
    End Sub

    ' Methods

    Private Sub CompressFile()
        Me._Crc1 = Me.DoCrc(Me._FileToCompress)
        Me._CompressedFile = (Me._FileToCompress & ".compressed")
	If Not Me._CompressedFile.StartsWith("unpack\") Then
	    Me._CompressedFile = Path.Combine("unpack", Me._CompressedFile)
	End If
	If Not Directory.Exists("unpack") Then
	    Directory.CreateDirectory("unpack")
	End If
	Console.WriteLine(ChrW(10) & "Creating compressed file: {0}", Me._CompressedFile)
        Using input As Stream = File.OpenRead(Me._FileToCompress)
            Using raw As FileStream = File.Create(Me._CompressedFile)
                Dim compressor As Stream = Nothing
                Try
                    if Me._WantZlib Then
                      compressor = New ZlibStream(raw, CompressionMode.Compress, True)
                    Else
                      compressor = New DeflateStream(raw, CompressionMode.Compress, True)
                    End If
                    Console.WriteLine("Using {0} to compress...", compressor.GetType.FullName)
                    Dim working As Byte() = New Byte(WORKING_BUFFER_SIZE ) {}
                    Dim n As Integer = -1
                    Do While (n <> 0)
                        If (n > 0) Then
                            compressor.Write(working, 0, n)
                        End If
                        n = input.Read(working, 0, working.Length)
                    Loop
                Finally
                  If Not compressor Is Nothing Then
                      compressor.Dispose()
                  End If
                End Try
            End Using
        End Using
    End Sub


    Private Sub DecompressFile()
        Dim working As Byte() = New Byte(WORKING_BUFFER_SIZE) {}
        Dim n As Integer = -7
        Using s1 As Stream = File.OpenRead(Me._CompressedFile)
            Me._DecompressedFile = (Me._CompressedFile & ".decompressed")
            Console.WriteLine(ChrW(10) & "Creating decompressed file: {0}", Me._CompressedFile)
            Dim decompressor As Stream = Nothing
            Try 
                If Me._WantZlib Then
                    decompressor = New ZlibStream(s1, CompressionMode.Decompress, True)
                Else
                    decompressor = New DeflateStream(s1, CompressionMode.Decompress, True)
                End If
                Console.WriteLine("Using {0} to decompress...", decompressor.GetType.FullName)
                Using s2 As FileStream = File.Create(Me._DecompressedFile)
                    Do While ((n <> 0) AndAlso (n <> -1))
                        n = decompressor.Read(working, 0, working.Length)
                        Console.WriteLine("read {0} bytes", n)
                        If (n > 0) Then
                            s2.Write(working, 0, n)
                        End If
                    Loop
                End Using
	    Finally
	      If Not decompressor Is Nothing Then
		  decompressor.Dispose()
	      End If
            End Try
        End Using
        Me._Crc2 = Me.DoCrc(Me._DecompressedFile)
    End Sub


    Private Function DoCrc(ByVal filename As String) As Integer
        Dim working As Byte() = New Byte(WORKING_BUFFER_SIZE) {}
        Dim n As Integer = -1
        Dim result As Integer = 0
        Using a As Stream = File.OpenRead(filename)
            Using b As  New Ionic.Zip.CrcCalculatorStream(a)
                n = -1
                Do While (n <> 0)
                    n = b.Read(working, 0, working.Length)
                Loop
                Console.WriteLine("File:{0}  CRC32:0x{1:X8}", filename, b.Crc32)
                result = b.Crc32
            End Using
        End Using
        Return result
    End Function


    Public Sub Run()
        If (Me._FileToCompress Is Nothing) Then
            zlib1.Usage
        Else
            Me.CompressFile
            Me.DecompressFile
            If (Me._Crc1 = Me._Crc2) Then
                Console.WriteLine("The files match.")
            Else
                Console.WriteLine("The files DO NOT MATCH.")
            End If
        End If
    End Sub



    Public Shared Sub Main(ByVal args As String())
        Try 
	    Dim z as New zlib1(args)
            z.Run
        Catch exc1 As Exception
            Console.WriteLine("Exception: {0}", exc1.ToString)
            zlib1.Usage
        End Try
    End Sub


   Public Shared Sub Usage()
        Console.WriteLine("ZlibTest: compress a file using DeflateStream or ZlibStream." & ChrW(10))
        Console.WriteLine("Usage:" & ChrW(10) & "  ZlibTest [-zlib] <filetodeflate />")
        Console.WriteLine("    -zlib  == use ZlibStream instead of DeflateStream.")
    End Sub

End Class

 
End Namespace

Coordinator
Feb 24, 2009 at 9:07 PM
Krystal, I think I answered the question ("How to use ZlibStream from VB.NET?")  but I didn't really answer the question "What am I doing wrong?"

You said you wanted to load the ZIP file into SQL Server.  You DO NOT need a ZlibStream to do that, in fact I think you may be on the wrong track there.
The ZlibStream is a stream class that slurps in uncompressed data and spits out compressed data.  It is a lower-level class, not typically something you would use if you are creating and manipulating ZIP files.
The ZipFile class within DotNetZip uses the ZlibStream (actually the cousin, the DeflateStream), to compress the filedata that it inserts into a zip file.  But if you are creating a zip file, you would never need to directly interact with ZlibStream class or the DeflateStream class.

If what you want is to load a byte array into SQL Server, and the byte array should contain the contents of the zip file, then you have a couple options.
1. Create the zip file as normal, save it to a file on disk.  Then, read in the file from the disk, into a byte array in memory.  You would use System.IO.File.OpenRead() to do that, then read in all the data.  Then do your thing with SQL.  (I'll assume you know how to load a byte array / blob into SQL)
2. Create the zip file ALMOST as normal, but save it to a MemoryStream, which you can then easily convert into a byte array. Instead of calling ZipFile.Save() with the name of an on-disk file, call ZipFile.Save() with a MemoryStream object.

I don't believe, from your english description of what you want to accomplish, that you need the ZlibStream class. 


Feb 24, 2009 at 9:16 PM
Thank you for your help, Cheeso.  That's great that I can skip Zlib and just use the regular lib.  Thanks for the help with the two options for storing into SQL Server.

Krystal