分享

Native Zip file support in Delphi XE2 | TwoDesk Delphi Blog

 npkaida 2017-02-19

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.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多