Native Zip file support in Delphi XE2

On the heels of last week’s Delphi XE2 rumor roundup, I’m quite pleased to announce that I’ve been given permission from the nice folks at Embarcadero to blog quite openly about Delphi XE2, which, it turns out, I’ve been using for a little while now :).

There’s been quite a bit written about the cross-platform compilers, FireMonkey, writing iOS apps in Delphi, and other amazing things (and believe me, they are amazing), but there are some other really cool things that haven’t been getting much attention, and I’d like to point some of those things out. I admit, it will be hard to write about things like zip files when I’ve got the same Delphi application running on my PC, my Mac, and my iPad, but I’ll try to do them justice. So, without further ado…

System.Zip.pas file iconDelphi XE2 adds a TZipFile class to the RTL. It’s in the System.Zip unit, and as you might expect, it brings native zip file capabilities to Delphi. Here’s an [incomplete] list of public methods of TZipFile:

  • class function IsValid(ZipFileName: string): Boolean; //Is the file at ZipFileName a valid zip file?
  • class procedure ExtractZipFile(ZipFileName: string; Path: string); //Extract all files from ZipFileName into the folder at Path
  • class procedure ZipDirectoryContents(ZipFileName: string; Path: string); //Zip the contents of the folder at Path into a zip file at ZipFileName
  • procedure Open(ZipFileName: string {or TStream}; OpenMode: TZipMode); //Open a zip file to manipulate it
  • procedure Extract({several overloaded possibilities}); //Extract a single file from the zip file
  • procedure Add({several overloaded possibilities}); //Add a single file to the zip file
  • property FileCount: Integer; //total number of files in the zip file
  • property FileName[Index: Integer]: string; //Returns the name of a file in the zip file
There are additional methods for dealing with the compression type, zip file comments, and more.
Here’s a quick example of listing all of the files in a zip file into a TMemo:
ZipFile := TZipFile.Create; //Zipfile: TZipFile
try
  ZipFile.Open('C:\Path\to\file.zip', zmRead);
  for I := 0 to ZipFile.FileCount - 1 do
  begin
    S := ZipFile.FileNames[I]; //S: string
    Memo1.Lines.Add(S);
  end;
  ZipFile.Close;
finally
  ZipFile.Free;
end;

I’ve often thought about about using zip files as a “project” file in applications where a “project” makes sense as multiple files (for example, if you’re writing a WYSIWYG web page creator, it would make sense that your document would consist of the html text, along with some images, stylesheets, and other related documents). Delphi XE2’s new TZipFile would make is easy to use a zip file as a native file format in your application.

Check back here tomorrow for more cool new stuff in Delphi XE2.

Delphi XE2 is going to be awesome. Make it even more awesome with Castalia for Delphi, my collection of Delphi IDE enhancements. Try it free today!

Want to learn more about Delphi XE2? Visit a free RAD Studio XE2 World Tour Event.

13 Responses to Native Zip file support in Delphi XE2

  1. robin August 29, 2011 at 11:34 am #

    thank you,
    do you know if encryption methods are also available?

  2. MoreWood August 29, 2011 at 12:29 pm #

    Good to know about this new feature. Sometimes a problem with Delphi is the help.

    Memo1.Lines.Add(ZipFile.FileNames[I]);

  3. David August 29, 2011 at 3:10 pm #

    Also wondered, if standard encryption is supported.

  4. Mason Wheeler August 29, 2011 at 6:03 pm #

    So there’s a constructor with no parameters, and then a separate Open method? That’s kinda strange. Is there anything useful you can do with a TZipFile that’s been created but not opened yet?

  5. Ralf August 29, 2011 at 7:14 pm #

    Is there any support to extract data from a stream?

  6. Pitfiend August 29, 2011 at 7:21 pm #

    mmm…okey… that seems quite irrelevant if you think that very good zip managers exist for long time now. TurboPower Abbrevia is a good example, has lots of features and it is open source. I think that having new components in the palette is not as important as the ability to have stable, optimized and fast executables and of course cross compiler options.

  7. Greg August 29, 2011 at 7:50 pm #

    Will TZipFile handle adding and extracting streams?

  8. jacob August 29, 2011 at 11:05 pm #

    I’m not sure about Encryption, I’ll have to check that out. It does handle opening a stream – the Open() method takes a filename or a TStream.

  9. Eric August 30, 2011 at 1:28 am #

    The only question is whether or not it will be properly supported: they have a very poor record track for supporting integrated 3rd-party features, cf. GIF or PNG…

  10. Dmitri Popov August 30, 2011 at 1:36 am #

    Constructions like “ZipFile.FileNames[I]” make me doubt that folders are supported. They should have used long time tested approach of FindFirst/FindNext methods instead.

  11. Marco August 30, 2011 at 1:53 am #

    Will it handle only the normal zip format, not 7z? Because that would give it a boost; 7z is open, and does almost always better in compressing data than the standard zip format.

  12. Fajar Anggiawan October 18, 2011 at 2:54 am #

    No feature to delete a file from zip archive? Great :(

Trackbacks/Pingbacks

  1. Delphi XE2: 64bit, 3d GUI, MacOs, iOS en meer! | 4DotNet Developers Blog - September 2, 2011

    […] Daarnaast zijn ook veel andere kleine dingen verbeterd, zoals built-in support voor zip, een uitgebreide (remote) deployment manager, verbeterde remote debugging, etc. De “What’s […]

For programmers, by a programmer

Hi. My name is Jacob, and I'm the creator of Castalia.

I starting programming in 1986, learning Lightspeed Pascal on a Mac Classic. Today, I'm a professional programmer, teacher, and entrepreneur.

I have a Master's Degree in Computer Science, and I still love Pascal and Delphi.

I believe that writing code is the heart and soul of software development, and I love helping programmers write code more effectively.