共用方式為


Command-Line Compression in Core

Hi, Ned here again. Today I’m going to talk about handling compressed files in Windows Server 2008 running as a Core server. You know, that thing with no pretty graphical interface and just a big command prompt that stares at you, daring you to try some successful administration.

Core mode is pretty slick – super-small footprint on the disk, great memory utilization, and plenty of useful roles that can be installed. But sheesh – it can be a real pain in the tail if your command-line skills are rusty or if you just haven’t ever had to deal with certain operations sans GUI.

So, quick question: have you tried to zip a file on a Core server yet? It’s sorta hard… because there is no zip.

With Core, there is no Explorer, so there is no file compression functionality. What if you need to compress a file on your Core server in order to copy it over some awfully slow connection and you want to do it all locally? You have two options:

  1. Make sure you have one of the 400 billion trillion zillion command-line zipping tools installed on your Core servers. Many of which are free even for commercial use, if you are that ‘frugal’.
  2. Cheat like a pro, and use MAKECAB.EXE and EXPAND.EXE.

Let’s talk about pro-style cheating.

Compressing a single file with MAKECAB

MAKECAB is a tool present in all later SKU’s of Windows. While it cannot create ZIP files, it can create cabinet (CAB) files. It’s an odd little utility as it was designed for developers to package files for installs, but it works well with decent compression.

So let’s take the scenario where you are remoted into your Core server through Terminal Services, WINRM, or (yuck) Telnet. You have a single file you need to compress and copy elsewhere. In your CMD prompt session, type:

MAKECAB <source> <destination> /l <some folder path>

So for example:

MAKECAB c:somefoldersomebig.fil smaller.cab /l c:

This will cab up the ‘somebig.fil’ into ‘smaller.cab’ and put that file at the root of the C: drive for easier access.

Compressing a bunch of files with MAKECAB

Things get trickier with multiple files. Remember that MAKECAB was designed for developers, so naturally it’s as complex as possible :-). Using MAKECAB for a bunch of files requires that you use a directives file. Let’s say we have a folder full of files, and we want to zip them all up into one cab:

1.  CD into the folder containing your files.

2.  Run:

DIR /B > mycab.ddf

3.  Run:

MAKECAB /d cabinetnametemplate=smaller.cab /d maxdisksize=CDROM /f mycab.ddf

4.  You will now have a folder called ‘Disk1’ that contains ‘smaller.cab’

So a bit tricky, but not too terrible. If you want to get fancy you can add paths in a DDF file, and can even add more directives. If you want a definite cure for insomnia, you can read everything you ever wanted to know about cab files and the Cabinet SDK by going here. This includes further tools like CABARC that add advanced cabinet features. The "maxdisksize" switch there tells makecab.exe to write to the cab file up to 660MB. If there is still data to go, it will then create a "disk2" folder, write to that cab file, and so on.

Decompressing a cab file with EXPAND

Naturally, you’re going to want a way to decompress these files. If you had just copied to a Full Windows 2008 computer, Explorer is perfectly ok at reading CAB files. But let’s say you’ve been copying these CAB files between Core servers and now need to expand the data out. Simple:

EXPAND -R <cab file> -F:* <destination folder that must exist>

So using our smaller.cab from above:

EXPAND –R smaller.cab –F:* c:

And now our file(s) in the CAB file are decompressed and ready to roll. All without any fancy Explorer shell.

Notes

Final Raymond Chen-style preemptive strike: you’re probably firing up the Comment section below to lambast the developers about leaving zip tools out of Win2008 and how this is a conspiracy to… make people not like us? The real reason there is no command-line zip in Core is because there has never been a command-line zip in Windows. IT Pros just haven’t asked for it; they had their favorite zipping tools of choice, and there was always a GUI method for the home users. In fact, there was no MAKECAB in Core mode for most of its development history, because no MS beta customer had asked for it or for zip functionality after a year of testing! Yours truly raised a (polite, professional) stink and it was added in RC1. Don’t say I never did anything for you, folks.

- Ned Pyle

Comments

  • Anonymous
    September 30, 2008
    The comment has been removed

  • Anonymous
    October 01, 2008
    Ein Interessanter Artikel zu einem Problem, das ich in dieser Form noch nicht hatte: Wie packe (zippe)

  • Anonymous
    October 06, 2008
    196 Microsoft Team blogs searched, 85 blogs have new articles in the past 7 days. 194 new articles found