Results 1 to 13 of 13

Thread: Compression (ZIP) Library

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Compression (ZIP) Library

    The attached project is a library that wraps #ZipLib and provides a simple interface so you can compress to and extract from ZIP files with a single method call. I may put it in the UtilityBank eventually, but it's still a work in progress so I'll leave it here for now.

    Note that there are methods to compress a single file, a number of files and a folder, both recursively and not. There are also methods to extract all files, a single file or a number of selected files. At this point there is no facility to add files to an existing archive, but that will be added soon.

    Note that this library does nothing on its own. It requires the presence of #ZipLib, which you can download from here, to do the grunt work. The project contains a reference to #ZipLib but that will be broken when you try to use it. You will need to remove that reference and add a new one yourself once you have downloaded it.

    Feel free to use this code in full or in part in your own applications, but it must be in accordance with the #ZipLib licence. I will be making future updates, including what I mentioned earlier and support for other compression formats. Feel free to post any requests, suggestions or bug reports on this thread, but I make no guarantees about if and when they will be actioned.

    The attached project (VS.NET 2003 format) contains no binary files, so you will of course have to compile it before you use it. It also contains no sample project, as I think the exposed methods pretty much speak for themselves. Note that the root folder contains a documentation file named Wunnell.IO.Compression.chm that should tell you everything you need to know. If you have any issues feel free to post here, but again I make no guarantees about if and when I will be able to provide help.

    Enjoy!

    Updates:
    15092005: v1.0.0.1 - fixed bug in Zip.ExtractEntryByName and Zip.ExtractEntriesByName methods, updated documentation to include VB.NET syntax.
    Attached Files Attached Files
    Last edited by jmcilhinney; Aug 9th, 2008 at 09:11 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Compression (ZIP) Library

    Nice, this shall be used in projects to come, clients can never live without their precious zip files

    I would rate you, but apparently i have to spread some around

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compression (ZIP) Library

    I have fixed a bug in the ExtractEntryByName and ExtractEntriesByName methods and updated the project attached to post #1 to version 1.0.0.1. I have also updated the documentation to include VB.NET syntax. It was my first time using NDoc so I missed that the first time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: Compression (ZIP) Library

    This may prove useful for a project of mine.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

  5. #5
    New Member
    Join Date
    Sep 2005
    Posts
    8

    Re: Compression (ZIP) Library

    I'm a beginner (also in english) ... I've tried to use it but I've some problem with the "ExtractAllEntries" metod... If in the zip file there aren't any foder all works fine, if there are/is the metod apparently doesn't work ...

    Have you some suggestion?

  6. #6

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compression (ZIP) Library

    Quote Originally Posted by CaraCarogna
    I'm a beginner (also in english) ... I've tried to use it but I've some problem with the "ExtractAllEntries" metod... If in the zip file there aren't any foder all works fine, if there are/is the metod apparently doesn't work ...

    Have you some suggestion?
    I just tested it with folders and it worked fine. I created a form with two buttons and added this code:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim fbd As New FolderBrowserDialog
    3.  
    4.         If fbd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.             Wunnell.IO.Compression.Zip.CompressFolder(fbd.SelectedPath)
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    10.         Dim ofd As New OpenFileDialog
    11.  
    12.         If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
    13.             Wunnell.IO.Compression.Zip.ExtractAllEntries(ofd.FileName)
    14.         End If
    15.     End Sub
    I then created a new folder under My Documents called "Test Folder" and added two new text files and a new folder, accepting the default names. I then opened the subfolder and added a further two text files with default names. I then ran my project, clicked Button1 and selected the "Test Folder" I created. This created a ZIP file under My Documents named "Test Folder.zip", which I opened and can confirm that it contained all the expected files with the expected paths. I then renamed the folder I created to "xText Folder" and clicked Button2. I selected "Test Folder.zip" and it recreated the original folder structure faithfully.

    What exactly is happening that shouldn't when you try it? What overload of ExtractAllEntries are you using? Have you read the documentation to make sure that it is creating the folders where you expect? Was this ZIP file created using this library or by some other tool?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    Sep 2005
    Posts
    8

    Re: Compression (ZIP) Library

    I found some problem in :
    VB Code:
    1. Public Overloads Shared Function ExtractEntriesByIndex(ByVal archiveName As String, ByVal destinationFolder As String, _
    2. ByVal indices As Integer(),  ByVal includePath As PathType,  ByVal overwrite As Boolean, ByVal verify As Boolean) As Boolean
    During my test something was going wrong while the code was moving all the temporary files to their final destination.
    The "filesExtracted" index was not corectly calculated ... I've thought that the problrm was the istruction:
    VB Code:
    1. File.Move(tempName, fileNames(tempName))
    when was processing folder an exception was generated and the "filesExtracted" calculation skipped, if that value is different from indices.Length value all the extracted entry are deleted.
    Due to this fact I've modified the code adding a condition:
    VB Code:
    1. If Not (Len(Path.GetDirectoryName(fileNames(tempName))) = Len(fileNames(tempName)) - 1) Then
    2.           File.Move(tempName, fileNames(tempName))
    3. End If

  8. #8

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compression (ZIP) Library

    I might have a better chance of diagnosing the problem if you'd told me exactly what the exception was, i.e. type and error message.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    New Member
    Join Date
    Sep 2005
    Posts
    8

    Re: Compression (ZIP) Library

    You're right ... my post was not very accurate, but I've left that project long time ago and I don't remember exactly what was going wrong...
    Using the original Zip.vb I've tryed to Extract the Dir1.zip file that it's just a zip with the following structure:

    Dir1
    |---Dir1a
    | |--New Text Document.txt
    |
    |---Dir1b
    | |--New Text Document.txt
    |
    |--New Text Document.txt

    I've created a button and I've inserted the following code in the click event:

    VB Code:
    1. SwitchSimaticITVer.Compression.Zip.ExtractAllEntries("C:\TEMP\b\dir1.zip", _
    2. "C:\TEMP\b", Compression.Zip.PathType.Relative, True, True)

    in order to display the exception message I looked for:"File.Move(tempName, fileNames(tempName))" in zip.vb module, after the catch I inserted "e As Exception" and instead of "'Do nothing." I inserted "MsgBox(e.Message)"
    After I pushed the button I've got a message :"Cannot create a file when that file already exist."
    Due to this fact I've modified the code in this way:
    VB Code:
    1. ... zip.vb code
    2.     If Not (Len(Path.GetDirectoryName(fileNames(tempName))) = Len(fileNames(tempName)) - 1) Then
    3.         File.Move(tempName, fileNames(tempName))
    4.     End If
    5.  
    6.     filesExtracted += 1
    7. End While
    8. ... zip.vb code

  10. #10
    Member
    Join Date
    Sep 2005
    Posts
    49

    Re: Compression (ZIP) Library

    Does anyone know of a similar class for iso files ? Thanx

  11. #11
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Compression (ZIP) Library

    I think the code here may need to be updated, was trying to use it today and noticed a lot of errors revolving around ZipNameTransform. I believe from looking at the class in the newest release of #ZipLib that ZipNameTransform no longer uses three different transform methods. The only variable that can be passed to it is string TrimPrefix.

    I havn't worked out exactly how it should be used now but all the code dealing with New ZipNameTransform(Boolean, String), needs to be modified. Here is a link to the thread I made in VB .NET forum discussing the problem with some of my observations:

    http://www.vbforums.com/showthread.php?t=495044
    Last edited by StevenHickerson; Nov 1st, 2007 at 11:45 AM.

  12. #12
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Compression (ZIP) Library

    Ok think I found a solution to this, they changed it so that the TrimPrefix string is basically allowing you to store as many levels of the path as you determine that you want to.

    So here is a simple fix that will only include the highest level of the path if you tell it to include relative path.

    This is the new CompressFolder function in Zip.vb

    vb Code:
    1. Public Overloads Shared Function CompressFolder(ByVal sourceName As String, _
    2.                                                         ByVal archiveName As String, _
    3.                                                         ByVal recursive As Boolean, _
    4.                                                         ByVal excludeHidden As Boolean, _
    5.                                                         ByVal level As CompressionLevel, _
    6.                                                         ByVal includePath As PathType, _
    7.                                                         ByVal overwrite As Boolean, _
    8.                                                         ByVal testData As Boolean) As Boolean
    9.  
    10.             Dim transform As ZipNameTransform
    11.             Dim relPath As String
    12.             Dim pSplit() As String
    13.             Dim i As Integer
    14.             If Right(sourceName, 1) = "\" Then sourceName = Left(sourceName, Len(sourceName) - 1)
    15.             pSplit = Split(sourceName, "\")
    16.             relPath = pSplit(0)
    17.             If UBound(pSplit) >= 2 Then
    18.                 For i = 1 To UBound(pSplit) - 1
    19.                     relPath = relPath & "\" & pSplit(i)
    20.                 Next
    21.             Else
    22.                 relPath = relPath & "\"
    23.             End If
    24.  
    25.             'Create the appropriate name transform.
    26.             Select Case includePath
    27.                 Case PathType.None
    28.                     transform = Nothing
    29.                 Case PathType.Relative
    30.                     transform = New ZipNameTransform(relPath)
    31.                 Case PathType.Full
    32.                     transform = New ZipNameTransform
    33.             End Select
    34.  
    35.             Return CompressFiles(GetFiles(sourceName, recursive, excludeHidden), _
    36.                                  archiveName, _
    37.                                  level, _
    38.                                  transform, _
    39.                                  overwrite, _
    40.                                  testData)
    41.         End Function

    This might not be the cleanest way to do it but it works. Do about the same for compressFiles, but make sure you are doing it in the right one.
    vb Code:
    1. Public Overloads Shared Function CompressFiles(ByVal sourceNames As String(), _
    2.                                                         ByVal archiveName As String, _
    3.                                                         ByVal level As CompressionLevel, _
    4.                                                         ByVal includePath As PathType, _
    5.                                                         ByVal overwrite As Boolean, _
    6.                                                         ByVal testData As Boolean) As Boolean
    7.             Dim transform As ZipNameTransform
    8.             Dim relPath As String
    9.             Dim pSplit As String()
    10.             Dim i As Integer
    11.             Dim cRoot As String
    12.  
    13.             cRoot = GetCommonRoot(sourceNames)
    14.  
    15.             If Right(cRoot, 1) = "\" Then cRoot = Left(cRoot, Len(cRoot) - 1)
    16.             pSplit = Split(cRoot, "\")
    17.             relPath = pSplit(0)
    18.             If UBound(pSplit) >= 2 Then
    19.                 For i = 1 To UBound(pSplit) - 1
    20.                     relPath = relPath & "\" & pSplit(i)
    21.                 Next
    22.             Else
    23.                 relPath = relPath & "\"
    24.             End If
    25.  
    26.             'Create the appropriate name transform.
    27.             Select Case includePath
    28.                 Case PathType.None
    29.                     transform = Nothing
    30.                 Case PathType.Relative
    31.                     transform = New ZipNameTransform(relPath)
    32.                 Case PathType.Full
    33.                     transform = New ZipNameTransform
    34.             End Select
    35.  
    36.             Return CompressFiles(sourceNames, _
    37.                                  archiveName, _
    38.                                  level, _
    39.                                  transform, _
    40.                                  overwrite, _
    41.                                  testData)
    42.         End Function(sourceNames))

    Also you have to go through and any where there is a New ZipNameTransform([True / False) just change it to New ZipNameTransform, I'm not sure what this boolean was representing when this was written but it apparently is not needed anymore.

    Again this is just my quick fix, if the original author looks at it again he will probably have a better solution.
    Last edited by StevenHickerson; Nov 1st, 2007 at 02:53 PM.

  13. #13

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Compression (ZIP) Library

    Thanks for the input. It is quite a while since I've looked at this so maybe I will create an updated version soon for the most recent version of #ZipLib.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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