Zip files without ANY 3rd party controls (.NET 2.0)
Lots of people have asked about zipping files up via code. There are solutions that involve 3rd party components, running winzip or winrar in a command line silently, etc...
some of these work just fine, but what if you want to do it from with the framework itself, without leaving the .NET framework.
Well if VB had a ZIP class, we would all know about it already... but how about JAVA? J# is a built in part of the .NET framework, and it also has ZIP capabilities built right in. So by adding a reference to one of the .NET java libraries (vjslib.dll) you can add zip support.
I have created a class in VB.NET that will do just this. Attached is a sample project to show you how it is done.
I plan to add more functionality to the class when I have time, but most of the time, people are just looking to ZIP files up, and this will do just that.
the method takes only 2 params, one is a string array of the files you want to zip, and the other is the name of the zip file.
You should pass full paths (not just file names) for both params. The sample project will demonstrate how to use it. I hope this is useful to other people.
REMARKS:
I have not tested this on older .NET framework versions, and I have heard there might be issues if used on .NET 1.0 or 1.1, when zipping exe files. I can not confirm this just yet.
The sample project has a test.doc and test.txt in the debug bin folder. These are the files that the sample project zips up. They are simply docs that contain the alphabet a whole bunch of times (to give the file some substance so it can be compacted)
Re: Zip files without ANY 3rd party controls (.NET 2.0)
For those interested, if you wanted to include the complete filepath of a file when using this code (instead of just the filename), you need to make a minor change. In the ZipUtility.vb file, Find this line of code:
VB Code:
MyZipEntry = New ZipEntry(New IO.FileInfo(sCompleteFilePath).Name)
Change it to read as so:
VB Code:
MyZipEntry = New ZipEntry(New IO.FileInfo(sCompleteFilePath).FullName)
Re: Zip files without ANY 3rd party controls (.NET 2.0)
I just wanted to point out that the java lib files are not deployed with the 2.0 framework. They are deployed with Visual Studio....so....to all looking at this for "Zip files without ANY 3rd party controls" don't waste your time as the java lib files will pad your installation by roughly 1500KB. I'm sure a 3rd party zip component is out there that has a much smaller foot print.
Re: Zip files without ANY 3rd party controls (.NET 2.0)
they aren't deployed with the .NET framework, but they aren't only deployed with a visual studio isntall. MS does have a redist package you can include with the .NET 2.0 framework.
I agree there may be a component that has a smaller footprint, but most decent ones I have seen are at a cost. I would hope built in zip classes will make their way right into the standard framework before long though..
The 2.0 framework already did add a GZip class that is right in system.dll in the System.IO.Compression namespace...
Re: Zip files without ANY 3rd party controls (.NET 2.0)
Hi Kleinma, I am using your project to deal with a large number of zip files and all is working great. But I have now hit a problem with a corrupted zip file. The decompress fails and when i try to move or delete the file i get an error saying the file is locked by another process. This obviously gets cleared when the program is exited, but i need to deal with this in code.
Re: Zip files without ANY 3rd party controls (.NET 2.0)
I had only one problem with this. In the Private Overloads Sub Compress() method, I had to change the last ElseIf and comment out the If part:
Else 'If (fsInfo.Attributes And IO.FileAttributes.Archive) <> 0 Then
Because it was skipping one of my files.
Also, I changed the "paths" variable from an array of String to an ArrayList. The ArrayList makes it easier to dynamically add file paths to the list of files.
Re: Zip files without ANY 3rd party controls (.NET 2.0)
Originally Posted by jsweeney
I had only one problem with this. In the Private Overloads Sub Compress() method, I had to change the last ElseIf and comment out the If part:
Else 'If (fsInfo.Attributes And IO.FileAttributes.Archive) <> 0 Then
Because it was skipping one of my files.
Also, I changed the "paths" variable from an array of String to an ArrayList. The ArrayList makes it easier to dynamically add file paths to the list of files.
If you're using .Net 2.0 forget about the lame ArrayList, use List(Of String) instead (or List(of Integer), etc...)
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Re: Zip files without ANY 3rd party controls (.NET 2.0)
I have a case where my users require to keep the original file's Date/Time Modified value unchanged in the ZIP file after the process occurs... I have the code you supplied embedded and functioning wonderfully, but as I mentioned, the files within the ZIP contain the Date/Time of the actual ZIPPING process, not their originals...
Re: Zip files without ANY 3rd party controls (.NET 2.0)
No. I don't plan on any further work on this code example because it used the J# runtime which has since been discontinued from Microsoft's language line, and will have no future development down the line from Microsoft.
Re: Zip files without ANY 3rd party controls (.NET 2.0)
Originally Posted by kleinma
this thread is really old. I wouldn't consider the original code to be a viable solution anymore...
What would you recommend doing now instead? I know there is a Packaging namespace in .NET 3.0 and above, but I'm writing a .NET 2.0 app and trying to stay away from 3rd party controls to save money and confusion. I like the example. It works great and it was easy to implement too.