1. Why do I get invalid output when I try to save a zero length file from an archive (at *** Copy to subfile)? MapPack.TocEntries.Length = 0. It saves the whole 352,098 byte source file to the target rather than an empty file.
2. What is the correct way to treat path separators (at *** Path separator)?
While I am at it ...
3. Any advice on how to program for errors?
4. Any obvious memory leaks with this code?
procedure TForm1.Button3Click(Sender: TObject);
// Uses: Form1: TForm1; MapPack: TToc;
var
i: integer; // subfile counter
sUnpackPath: string; // Unpack path
fMap, fTgt: TFileStream; // Map & target files
begin
Form1.ProgressBar1.Visible := True; // Start ProgressBar
Form1.ProgressBar1.Max := MapPack.NumOfFiles-1;
fMap := TFileStream.Create(OpenDialog1.FileName, fmOpenRead); // Open map file
try
{Prepare directory for unpacked files}
sUnpackPath := OpenDialog1.FileName+'_unpacked\'; // *** Path separator
if not DirectoryExists(sUnpackPath) then
ForceDirectories(sUnpackPath);
for i := 0 to MapPack.NumOfFiles-1 do // For all subfiles
begin
fTgt := TFileStream.Create(sUnpackPath + MapPack.TocEntries[i].Name + '.'
+ MapPack.TocEntries[i].Extension, fmCreate); // Create subfile
fMap.Seek(MapPack.TocEntries[i].Offset, soFromBeginning); // Find subfile start
begin
fTgt.CopyFrom(fMap,MapPack.TocEntries[i].Length); // *** Copy to subfile
end;
fTgt.Free; // Close subfile
Form1.ProgressBar1.Position := i + 1; // Update ProgressBar
end;
except
// *** process errors
on e: exception do
ShowMessage(e.Message);
end;
fMap.Free; // Close map file
Form1.ProgressBar1.Visible := False; // Close ProgressBar
Form1.Button3.Enabled := False; // Unpack button
Form1.Button4.Enabled := True; // Save map button
end;