• ZIP Files (VB .NET 2005)

    Home » Forums » Developers, developers, developers » DevOps Lounge » ZIP Files (VB .NET 2005)

    Author
    Topic
    #446287

    I have a program that extracts data from a database, sticks it into an Excel workbook, zips the file using the GZipStream class in the System.IO.Compression namespace and then e-mails it to various people both in and out of our organisation. It works fine but there is one anomoly that I have worked around but would like a better solution for so I have come prostrate to the fount of all knowledge, Woody’s Lounge.

    I use this code for the compression of my Excel file

    Private Sub Compress(ByVal filename As String)

    ‘ Open the file as a FileStream object.
    Dim infile As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)

    Dim count As Integer

    Dim buffer(infile.Length – 1) As Byte
    ‘ Read the file to ensure it is readable.
    count = infile.Read(buffer, 0, buffer.Length)

    If count buffer.Length Then
    infile.Close()
    Throw New Exception(“Compression failed. Number of bytes read not equal to buffer size”)
    End If

    infile.Close()

    ‘ Create the memory stream to accept the compressed data
    Dim ms As New MemoryStream()
    ‘ Use the newly created memory stream for the compressed data.
    Dim compressedzipStream As New GZipStream(ms, CompressionMode.Compress, True)

    compressedzipStream.Write(buffer, 0, buffer.Length)

    ‘ Close the Zip stream.

    compressedzipStream.Close()

    ‘ The memory stream now contains the Zipped data

    ‘ Create the output stream for the new file.

    IO.File.Delete(filename & “.zip”)

    Dim outfile As New FileStream(filename & “.zip”, FileMode.Create, FileAccess.Write, FileShare.Write)

    Dim outbuffer(ms.Length – 1) As Byte
    ‘ Position to the start of the memory stream.
    ms.Position = 0

    count = ms.Read(outbuffer, 0, ms.Length)

    If count outbuffer.Length Then
    Throw New Exception(“Error reading memory stream. Count(” & count.ToString & ” not equal to buffer size(” & outbuffer.Length.ToString & “)”)
    End If

    outfile.Write(outbuffer, 0, outbuffer.Length)

    outfile.Close()

    End Sub

    Most of the code is straight out of the Help facility and it works fine creating a file called, for example, kevin.xls.zip from a file called kevin.xls. When you open the file (I use WinRar), the file inside the zip file is named the same as the zip file minus the .zip extension. You can then extract this file and open it with Excel without any problems. So, what’s my question?

    I can’t for the life of me figure out how to compress a file and give the resulting zip file a different name. For example, I want to zip ‘Some Transactions for 01-November-2007.xls’ into a zip file called fred.zip. Actually, you can do that but when you open the resulting zip file the file inside is called fred.

    Does anybody know how to do this?

    Regards,
    Kevin Bell

    Viewing 0 reply threads
    Author
    Replies
    • #1084841

      Hi Kevin,
      My guess is probably too obvious to be the right answer, but I’ll give it a shot anyway.

      It looks like the name of the internal file is passed when creating the first FileStream object (infile, in your example). It looks like the name of the external zip file is created with the second FileStream (outfile). I’m just guessing that the two names do not necessarily need to be identical…

      Hope this helps.

      • #1084866

        Thanks Mark. Indeed the two names don’t have to be identical. However, the Internal file name that shows in the archive file appears to be the archive file name without the .zip extension. If I create a file called, let’s say Some Transactions.xls.zip and then open it I see a file inside called Some Transactions.xls

        If I rename this archive file to Some Other Transactions.xls.zip and open it I see the file inside called Some Other Transactions.xls

        It appears there is no way to explicitly name the object held inside the archive file. From what I’ve read in the documentation, there appears to be no way to add more than one file to an archive. Yes, I could just keep adding the data from more than one file but I think it would just show up as a single file within the archive.

        At the end of the day at least I have a work around. By setting the name of the archive file to the same as the file inside with .zip appended to it my e-mail recipients can open the archive and retrieve the file. It’s just annoying that’s all that Microsoft in their usual infinite wisdom have once again only done half a job with the IO.Compression namespace.

        Regards,
        Kevin Bell

        • #1084925

          Kevin,

          Thanks for asking this question! thankyou

          We’ll keep this in mind as we go forward to our impending upgrade next year. Maybe this will be fixed sometime in the future….

    Viewing 0 reply threads
    Reply To: ZIP Files (VB .NET 2005)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: