The new archive manager in the latest Asphyre 4 snapshots is very flexible. I could have used something that needed a DLL, but instead I decided to reuse the
Turbo Power Abbrevia library, which was released under the MPL1.1 license. You do not need to worry about application bloat due to the size of the library, because the small unit I have put together only takes advantage of the UnZip functionality. My demonstration application only takes about 500kb.
Unlike the current implementations of 7-Zip and SQX, this Zip archive extension can extract to any stream. Because of this loading is marginally faster. As the Archive functionality advances I will update this unit to include password protection functionality.
The only line of code you must add to your game is, aside from
using the unit, this:
Code:
RegisterZip('.zip');
And you can change the extension to anything you so desire.
For those who want just the unit, it is under the MPL license. The archive with an example program you may
download here (1.5mb). Thanks go to Yuriy and Huasoft for helping me get my demo application rendering, as this was the first beta attempt of mine.
Code:
unit AsphyreArcZip;
//---------------------------------------------------------------------------
// AsphyreArcZip.pas Modified: 19-Jan-2007
// Archive Wrapper for ZIP file format Version 1.0
// Author: Robert "The Wicked Flea" Kosek
// http://www.thewickedflea.com
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is AsphyreArcZip.pas
//
// The Initial Developer of the Original Code is Robert Kosek
// Portions created by the Initial Developer are Copyright (C) 2007
// the Initial Developer. All Rights Reserved.
//---------------------------------------------------------------------------
interface
{$HINTS OFF}
uses Classes, SysUtils, AsphyreAsserts, MediaUtils, AsphyreArchives,
AbUnzper, AbZipTyp, AbExcept;
type
TAsphyreArchiveZip = class(TAsphyreCustomArchive)
protected
FArchive: TAbUnZipper;
function GetItemCount: Integer; override;
function GetItemName(Num: Integer): string; override;
function OpenArchive(const FileName: string): Boolean; override;
procedure CloseArchive(); override;
procedure DoCreate(); override;
destructor Destroy(); override;
public
function ExtractToDisk(const ItemName, DestPath: string): Boolean; override;
function ExtractToStream(const ItemName: string; Stream: TStream): Boolean; override;
end;
procedure RegisterZip(const Ext: string);
implementation
procedure RegisterZip(const Ext: string);
begin
ArchiveManager.RegisterExt(Ext, TAsphyreArchiveZip);
end;
{ TAsphyreArchiveZip }
procedure TAsphyreArchiveZip.CloseArchive;
begin
FArchive.CloseArchive;
end;
procedure TAsphyreArchiveZip.DoCreate;
begin
inherited;
FAttributes := [];
FArchive := TAbUnZipper.Create(nil);
end;
destructor TAsphyreArchiveZip.Destroy;
begin
with FArchive do begin
CloseArchive;
Free;
end;
inherited;
end;
function TAsphyreArchiveZip.ExtractToDisk(const ItemName, DestPath: string): Boolean;
var F: TFileStream;
begin
Result := True;
F := TFileStream.Create(MakeValidPath(DestPath) + MakeValidFileName(ItemName),
fmCreate or fmShareExclusive);
try
try
FArchive.ExtractToStream(ItemName,F);
except on E: EAbException do
begin
result := false;
raise e;
end;
end;
finally
F.Free;
end;
end;
function TAsphyreArchiveZip.ExtractToStream(const ItemName: string;
Stream: TStream): Boolean;
begin
Result := True;
try
FArchive.ExtractToStream(ItemName,Stream);
except on E: EAbException do
begin
Result := False;
raise E;
end;
end;
end;
function TAsphyreArchiveZip.GetItemCount: Integer;
begin
Assert((FArchive <> nil)and(FArchive.FileName <> ''), msgArchiveNotOpened);
Result := FArchive.Count;
end;
function TAsphyreArchiveZip.GetItemName(Num: Integer): string;
begin
Assert((FArchive <> nil)and(FArchive.FileName <> ''), msgArchiveNotOpened);
Result := FArchive.Items[Num].FileName;
end;
function TAsphyreArchiveZip.OpenArchive(const FileName: string): Boolean;
begin
Result := True;
try
FArchive.OpenArchive(FileName);
except on E: EAbException do
begin
Result := False;
raise E;
end;
end;
end;
end.