Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net
Natively zipping Files in VB.net Natively zipping Files in VB.net
Natively zipping Files in VB.net
Go Back  Xtreme Visual Basic Talk > > > > Natively zipping Files in VB.net


Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2007, 06:44 PM
PrOpHeT's Avatar
PrOpHeTNatively zipping Files in VB.net PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default Natively zipping Files in VB.net


Now this only works on XP/2003 server/(Not tested on vista, but should work)

Requires reference to shell32.dll in %windir%\system32

I recently had a scripting question concerning copying files with progress with VBS and dilettante was so kind as to point out that the shell object could be used to make explorer copy files instead of using typical file io.

I knew that the shell32 library could be utilized in .net as a com import, I have used it to manipulate windows before. So I decided to do a little playing.

A quick hex edit examination of a new zip file created in windows revealed that the file was simply a binary file with the header of “PK” and ENQ and an ACK character followed by a bunch of nulls, fairly easy to recreate to say the least.

Code:
Private Sub CreateZipFile(ByVal Filename As String) 'create a new empty zip file Dim Encoder As New System.Text.ASCIIEncoding Dim Header As String = "PK" & Chr(5) & Chr(6) Header = Header.PadRight(22, Chr(0)) My.Computer.FileSystem.WriteAllBytes(Filename, Encoder.GetBytes(Header), False) 'so what have we done here? 'we have created a blank zip file the same as right clicking and choosing 'new > Compressed (Zipped) folder, it must have the extension .zip for the 'trick to work. End Sub

Here is where the magic happens!
We use the explorer shell to copy the file instead of a typical file.io src/dest file copy. Explorer recognizes the destination is a compressed file and inserts the input file/directory into it compressing on the fly the same as if you had chosen copy paste in explorer. The method works if Input is a file or a directory.

Code:
Private Sub ZipFile(ByVal Input As String, ByVal Filename As String) Dim Shell As New Shell32.Shell CreateZipFile(Filename) Shell.NameSpace(Filename).CopyHere(Input) End Sub

Now we can call

Code:
'take everything from the C:\Testfrom directory and compress it into the c:\testzip.zip file ZipFile("c:\testfrom", "c:\testzip.zip")


Of course you could create your own method of enumerating files and folders to insert, this is just to show how they can be inserted into the compressed file.


I am still trying to figure out how to get the files back out, but if you simply need to archive something, prepare it for upload, etc... there seems no easier way and no thrid party controls needed.

What if you needed to zip from legacy VB?

Just as easy just some aditional code...

the folowing VBS file will do the same as the above .net code and can easily be ported to vb6

Code:
Compress "c:\testfrom" ,"c:\MyFilesZip.zip" Sub Compress(Input, ZipFile) Dim Shell : Set Shell = CreateObject("Shell.Application") Dim FSO : set FSO = CreateObject("Scripting.fileSystemObject") FSO.CreateTextFile(ZipFile, true).WriteLine "PK" & Chr(5) & Chr(6) & String(18, 0) Set ZipFile = Shell.NameSpace(ZipFile) <I know I shouldn't but it makes for less lines of code ;) ZipFile.CopyHere Input Do Until ZipFile.items.Count = 1 'important, makes the script not fall out and dispose of objects before they are done 'items.count is the amount of root items you anticipate to be in the zip file wscript.sleep 100 Loop Set Shell = Nothing Set FSO = Nothing Set ZipFile = Nothing End Sub


Now all of this is just rough concept at this point, but certainly you can see it has real potential. As I clean it up and wrap a real class around it with some flow and error control I will keep it posted, if anyone else plays with it and wants to contribute please do. This is a VERY common question here and worth the research.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...

Last edited by PrOpHeT; 06-12-2007 at 08:26 PM.
Reply With Quote
  #2  
Old 06-12-2007, 09:01 PM
PrOpHeT's Avatar
PrOpHeTNatively zipping Files in VB.net PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Ok guys here is the inverse, still in the rough, but growing every time I get a chance to work on it.

Imports system.io (For the directory operations)

Code:
Public Sub UnZipFile(ByVal FileName As String, ByVal Destination As String) If Not Directory.Exists(Destination) Then _ Directory.CreateDirectory(Destination) Dim myShell As New Shell32.Shell Dim Compressed As Shell32.Folder = myShell.NameSpace(FileName) Dim ExtractTo As Shell32.Folder = myShell.NameSpace(Destination) ExtractTo.CopyHere(Compressed.Items()) End Sub

Only major issues I have found thus far is windows will allow you to cancel if the operation lasts long enough to see the progress indicator (Maybe hide this with findwindow and a hide message in the future?)

And when compressing the progress indicator starts and stays at 100%, however I have tested and windows does this as well when copying to a compressed file, so not a code issue it appears to be a windows issue.

Still going for anyone watching progress, I will eventually clean it up and maybe have a mod delete the progression when the final tut is up.

As well I am working on the legacy version, I have nothing to base the extraction progresion on from a code standpoint.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...

Last edited by PrOpHeT; 06-12-2007 at 09:34 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net Natively zipping Files in VB.net
Natively zipping Files in VB.net
Natively zipping Files in VB.net
 
Natively zipping Files in VB.net
Natively zipping Files in VB.net
 
-->