Overview
TZipFix

Version 1.1

©2000 Angus Johnson

ajohnson@rpi.net.au

This component has been designed to repair damaged Zip files.

To avoid confusion the Zip file itself will be called 'the archive', and each file which has been zipped into this archive will be called a 'file'.

How does it work?

Firstly, the archive's main file directory is ignored and is presumed to be damaged.

The archive is scanned for the local header belonging to each file in the archive and checked for damage. If the local header info appears OK, then the related file is added to the new 'repaired' zip file. If the local header info is not OK then the related file is not added to the 'repaired' zip file.

Finally, the main directory is rebuilt using the information gathered from the local file headers.

Note: If the ‘main’ archive contains another ‘enclosed’ zip archive or archives, and the local header for this ‘enclosed’ archive is damaged, then the contents of this ‘enclosed’ archive can still be restored as long as the local headers within this archive are OK. In this case, however, although the data will be recovered, the files restored from the damaged ‘enclosed’ zip archive will be listed individually in, and accessed via the ‘main’ archive directory as the damaged ‘enclosed’ zip archive will no longer exist in the ‘fixed’ archive.

type

TOnFileFoundEvent = procedure (Sender: TObject;

  const Filename: string; FileInfoIsOK: boolean) of object;

TZipFix = class(TComponent)

public

  procedure Execute(InStream: TMemoryStream;

    OutStream: TStream); {$IFDEF VER130} overload;

  procedure Execute(const InFilename,

    OutFilename: string); overload; {$ENDIF}

published

  property OnFileFound: TOnFileFoundEvent;

end;

Component Use

If you have added this component to Delphi’s IDE component palette, then just add a TZipFix component to your form at designtime. Alternatively, it can be added dynamically at runtime by creating the TZipFix component in either the form’s OnCreate method or in the same method which calls TZipFix’s Execute method. (If you create the component dynamically, then it is important to destroy it too.)

Calling the Execute method is all that is required to repair a damaged Zip archive. In Delphi 5, this method is overloaded, so it can be passed 2 different parameter sets. In Delphi 3 & 4, the parameters must be: 1) the ‘damaged' zip file passed as a TMemoryStream; and 2) the target ‘fixed’ file passed as any descendant of TStream (eg TMemoryStream or TFileStream). In Delphi 5, the parameters can also be two strings indicating: 1) the damaged archive’s filename; and 2) the filename of the file to be created which will become the ‘fixed’ archive.

Delphi 5 example:

FixedFile := changefileext(

  OpenDialog1.filename, '_fixed.zip');

with TZipFix.create(self) do

try

  Execute( OpenDialog1.filename, FixedFile);

finally

  free;

end;

The OnFileFound event is designed to optionally give feedback to the end user as to which files within the archive can and cannot be successfully copied to the new ‘fixed’ archive. The name of each file found within the ‘damaged’ archive is passed as the Filename string parameter. If the filename cannot be determined, then an empty string is passed instead. The FileInfoIsOK boolean parameter indicates whether the named file can be successfully copied or not.

Example:

var

 OKCnt, TotalCnt: integer; //assume both reset to 0 prior

                           //to Execute() being called.

procedure TForm1.OnZipFileFound(Sender: TObject;

  const Filename: string; FileInfoIsOK: boolean);

var

 s: string;

begin

  inc(TotalCnt);

  if Filename = '' then

    s := '??filename??' else

    s := Filename;

  if FileInfoIsOK then

    s := s + #9' seems to be OK' else

    s := s + #9' is corrupted';

  if FileInfoIsOK then inc(OKCnt);

  memo1.lines.add(s);

end;


Licence Agreement
TZipFix

Version 1.1

©2000 Angus Johnson

ajohnson@rpi.net.au

Licence Agreement:

This component has been released as freeware.

No guarantee of performance is given. Any damage to software resulting from its use will be the responsibility of the user.

Please send any bug reports to the author at the above email address. All feedback is welcome.