views:

8475

answers:

9

Is the ZIP compression that is built into Windows XP/Vista/2003/2008 able to be scripted at all? What executable would I have to call from a BAT/CMD file? or is it possible to do it with VBScript?

I realize that this is possible using WinZip, 7-Zip and other external applications, but I'm looking for something that requires no external applications to be installed.

+1  A: 

There are various VBScript implementations available on Google, e.g. "Zip and UnZip Files Using the Windows Shell (XP, Vista, 2003 and 2008) and VBScript". I'm haven't tested these but it's most likely they only 'zip', not compress.

samjudson
what's the distinction you are drawing between zip and compress?
Cheeso
You can 'zip' files up into a single file without compressing them, much like 'tar' in unix. This allows you to distribute the files as a package, but does not reduce their size of disk space.
samjudson
*its most likely they only 'zip', not compress.* No, that's not right. The script there creates a zip file, with compression of the entries.
Cheeso
A: 

A cursory look in system32 yields nothing but a dll - I don't believe the explorer extension can be scripted, and I'm not sure how that would differ between XP and Vista.

One thing to note is that there is a command-line version of 7-zip that is packaged as a single exe, '7za.exe' - while I realize this may not be the exact answer you're looking for, it does not require installing another app, it just requires packaging the file with your program and its license.txt.

Joshua McKinnon
The explorer thinig can be scripted, and in fact when you script it, it even gives you an explorer-ish file copy dialog, with "flying pages".
Cheeso
Gladly corrected. Is it done through VBScript, or how? The accepted answer mentions VBA, but there is also a comment indicating there is some potential fragility...
Joshua McKinnon
+2  A: 

There are both zip and unzip executables (as well as a boat load of other useful applications) in the UnxUtils package available on SourceForge (http://sourceforge.net/projects/unxutils). Copy them to a location in your PATH, such as 'c:\windows', and you will be able to include them in your scripts.

This is not the perfect solution (or the one you asked for) but a decent work-a-round.

brian newman
A: 

There are .NET 2.0 interfaces to the built-in Windows compression; check out the System.IO.Compression namespace, there is implementation of GZip (Microsoft-only algorithm) and DeflateStream (standards-based algorithm). To use them, you will need to write streams, there are some good examples online.

You could set up a PowerShell script to zip and unzip files using these assemblies. Of course, you would have to install PowerShell and .NET 2.0 on client machines to implement this, so it may not solve your problem.

Guy Starbuck
Some corrections! I'm not sure what is meant by "GZip (Microsoft-only algorithm)" but...GZIP is not a Microsoft-only algorithm! GZIP is a file format defined in IETF RFC 1952, and it uses the DEFLATE algorithm as defined in IETF RFC 1951. The GZipStream class provides a stream that reads and writes the GZIP format. Neither the GZipStream class nor the DeflateStream class themselves can be used to read or write ZIP files.
Cheeso
+7  A: 

There are VBA methods to zip and unzip using the windows built in compression as well, which should give some insight as to how the system operates. You may be able to build these methods into a scripting language of your choice.

-Adam Davis

Adam Davis
Caveat: The posted scripts rely on implementation-dependent behavior of the Windows shell and, for example, can easily break when third party compression software changes the default OLE verbs for ZIP archives. Also, the zipfldr.dll COM objects are *not* documented or supported for this use by MS.
Mihai Limbășan
A: 

Personally I use 7-zip for my command line zipping needs. It supports standard .zip format, and also .7z format which in my experience also provides much better compression than .zip. The downside is that you need 7-zip to open .7z files.

Kibbee
+11  A: 

Yes, this can be scripted with VBScript. For example the following code can create a zip from a directory:

Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")


MyTarget = Wscript.Arguments.Item(0)
MySource = Wscript.Arguments.Item(1)

Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items

do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
    wscript.sleep 1000 
loop

Set winShell = Nothing
Set fso = Nothing

You may also find http://www.naterice.com/blog/template_permalink.asp?id=64 helpful as it includes a full Unzip/Zip implementation in VBScript.

Jay
A: 

There is a "compact" shell command for duing this

Daniel Silveira
This is just for marking (or removing) the NTFS compression flag. It does not create zip files.
Goyuix
+2  A: 

Just for clarity: GZip is not an MS-only algorithm as suggested by Guy Starbuck in his comment from August. The GZipStream in System.IO.Compression uses the Deflate algorithm, just the same as the zlib library, and many other zip tools. That class is fully interoperable with unix utilities like gzip.

The GZipStream class is not scriptable from the commandline or VBScript, to produce ZIP files, so it alone would not be an answer the original poster's request.

The free DotNetZip library does read and produce zip files, and can be scripted from VBScript or Powershell. It also includes command-line tools to produce and read/extract zip files.

Here's some code for VBScript:

dim filename 
filename = "C:\temp\ZipFile-created-from-VBScript.zip"

WScript.echo("Instantiating a ZipFile object...")
dim zip 
set zip = CreateObject("Ionic.Zip.ZipFile")

WScript.echo("using AES256 encryption...")
zip.Encryption = 3

WScript.echo("setting the password...")
zip.Password = "Very.Secret.Password!"

WScript.echo("adding a selection of files...")
zip.AddSelectedFiles("*.js")
zip.AddSelectedFiles("*.vbs")

WScript.echo("setting the save name...")
zip.Name = filename

WScript.echo("Saving...")
zip.Save()

WScript.echo("Disposing...")
zip.Dispose()

WScript.echo("Done.")

Here's some code for Powershell:

[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");

$directoryToZip = "c:\\temp";
$zipfile =  new-object Ionic.Zip.ZipFile;
$e= $zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.")
$e= $zipfile.AddDirectory($directoryToZip, "home")
$zipfile.Save("ZipFiles.ps1.out.zip");

In a .bat or .cmd file, you can use the zipit.exe or unzip.exe tools. Eg:

zipit NewZip.zip  -s "This is string content for an entry"  Readme.txt  src 
Cheeso