Recent

Author Topic: Error Extracting Empty Files From Archive File  (Read 3216 times)

flywire

  • Jr. Member
  • **
  • Posts: 85
Error Extracting Empty Files From Archive File
« on: April 06, 2015, 03:54:39 pm »
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?

Code: [Select]
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;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Error Extracting Empty Files From Archive File
« Reply #1 on: April 07, 2015, 03:47:31 am »
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[ i ].Length = 0. It saves the whole 352,098 byte source file to the target rather than an empty file.

That's by design according to the source code:
Code: [Select]
       BufferSize:=MaxSize;
       if (Count>0) and (Count<BufferSize) then
         BufferSize:=Count;    // do not allocate more than needed

Simply don't call CopyFrom when MapPack.TocEntries[ i ].Length = 0

2. What is the correct way to treat path separators (at *** Path separator)?

Use DirectorySeparator constant:
Code: [Select]
  somePath := BasicPart + '_unpacked' + DirectorySeparator;

or IncludeTrailingPathDelimiter function:
Code: [Select]
  somePath := IncludeTrailingPathDelimiter(BasicPart + '_unpacked');

3. Any advice on how to program for errors?

4. Any obvious memory leaks with this code?

I'll leave these to more experienced members, if they be kind enough to give us their opinions.

flywire

  • Jr. Member
  • **
  • Posts: 85
Re: Error Extracting Empty Files From Archive File
« Reply #2 on: April 07, 2015, 10:05:51 am »
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[ i ].Length = 0. It saves the whole 352,098 byte source file to the target rather than an empty file.

That's by design according to the source code:

Well if I asked the bank to move zero money to my account and they gave me all of the money in the bank then it would be considered a bug (and I would move to South America with the cash). It's a bug.



3. Any advice on how to program for errors?

4. Any obvious memory leaks with this code?

I'll leave these to more experienced members, if they be kind enough to give us their opinions.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Error Extracting Empty Files From Archive File
« Reply #3 on: April 07, 2015, 11:43:13 am »
That's by design according to the source code:

Well if I asked the bank to move zero money to my account and they gave me all of the money in the bank then it would be considered a bug (and I would move to South America with the cash). It's a bug.

Well now you know the secret, if you ask your bank to move zero money to your account they will give you all the money, but unfortunately no cash!

 

TinyPortal © 2005-2018