Recent

Author Topic: SOLVED Pack Files into one and extract  (Read 523 times)

DreamVB

  • New Member
  • *
  • Posts: 21
SOLVED Pack Files into one and extract
« on: June 27, 2022, 11:00:01 pm »
Hi, Can someone help me please I am looking for a way to pack a list of files into one file, so that I can extract them. I am trying to make my own little setup program, I don't need compression just something to put the files into one, something like the PAK format. I remember years ago I had a class to work with PAK files on a Delphi cd but I lost it. I tried looking on google and can't seem to find anything, if you can help I be very happy thanks.
« Last Edit: June 28, 2022, 08:40:34 pm by DreamVB »
When your dreams come true

wp

  • Hero Member
  • *****
  • Posts: 9906
Re: Pack Files into one and extract
« Reply #1 on: June 27, 2022, 11:03:42 pm »
Although you do not request compression, I'd use the zipper unit nevertheless: https://wiki.freepascal.org/paszlib#Examples

DreamVB

  • New Member
  • *
  • Posts: 21
Re: Pack Files into one and extract
« Reply #2 on: June 28, 2022, 01:56:38 am »
Can someone have a look at my project please I sort of got something packing the files into a bag file but I am having problems extract it only seems to extract one file. I added the project with this post

Thanks
When your dreams come true

Thaddy

  • Hero Member
  • *****
  • Posts: 11772
Re: Pack Files into one and extract
« Reply #3 on: June 28, 2022, 08:46:46 am »
The usual case of case?
Black themes should be banned.

Roland57

  • Sr. Member
  • ****
  • Posts: 287
Re: Pack Files into one and extract
« Reply #4 on: June 28, 2022, 12:40:45 pm »
Hello! I think the current code doesn't read the right number of bytes for each file. I would try something like this.

Code: Pascal  [Select][+][-]
  1.     while fileheader.fSize > sizeof(TBuffer) do
  2.     begin
  3.       blockread(BagFile, TBuffer, sizeof(TBuffer), bRead);
  4.       blockwrite(outfile, TBuffer, bRead, bWrote);
  5.       Dec(fileheader.fSize, sizeof(TBuffer));
  6.     end;
  7.     blockread(BagFile, TBuffer, fileheader.fSize, bRead);
  8.     blockwrite(outfile, TBuffer, bRead, bWrote);

Here, it seems to work.

P.-S. I removed this line:

Code: Pascal  [Select][+][-]
  1. Seek(BagFile, filepos(BagFile) + FileHeader.fSize);
« Last Edit: June 28, 2022, 12:43:20 pm by Roland57 »

wp

  • Hero Member
  • *****
  • Posts: 9906
Re: Pack Files into one and extract
« Reply #5 on: June 28, 2022, 01:21:31 pm »
I guess the issue is in the y loop of Button2Click (Extract): You provide a buffer with 10240 bytes, and if the file is larger you want to read/write in parts, until all is done. you calculate the number of iterations needed as FileSize div 10240, but begin the loop with y=1. This is a problem when the file is smaller than 10240 bytes - now the loop is not executed at all (for y := 1 to 0). Keep in mind: working with 1-based loops has many disadvantages, better to use a 0-based loop:

Code: Pascal  [Select][+][-]
  1.     for y := 0 to fileheader.fSize div 10240-1 do
  2.     begin
  3.       blockread(BagFile, TBuffer, sizeof(TBuffer), bRead);
  4.       blockwrite(out_file, TBuffer, bRead, bWrote);
  5.     end;  

To get accustomed to the 0-based style I'd also propose to declare TBuffer as array[0..10240-1] (rather than array[1..10240]) - it absolutely is unimportant here, but in case of dynamic arrays you always have 0-based indices. For me personally it is the most annoying mental exercise to switch between 1-based and 0-based thinking.

I'd also recommend to call ForceDirectories(lzExtractTo) after assigning a value to lzExtractTo, in order to make sure that the destination directory exists (along with all intermediate folders).
« Last Edit: June 28, 2022, 01:23:37 pm by wp »

DreamVB

  • New Member
  • *
  • Posts: 21
SOLVED Pack Files into one and extract
« Reply #6 on: June 28, 2022, 08:38:56 pm »
Thanks Roland57 that seems to work it packing and extracting now how I want it to, thanks to all the rest that helped me to.

I now made a small class and I thought I share it an case someone else needs something like this. also now extracts with full paths.

« Last Edit: June 28, 2022, 11:37:03 pm by DreamVB »
When your dreams come true

 

TinyPortal © 2005-2018