Recent

Author Topic: Decompressing a gz file  (Read 425 times)

justnewbie

  • Sr. Member
  • ****
  • Posts: 278
Decompressing a gz file
« on: July 02, 2022, 05:56:43 pm »
Hi guys,
I have a *.gz file (a compressed *.csv) and I need to decompress it.
May I ask a code example?
My attempt using Abbrevia library is shown below (this gives an error: ... exception class 'EAbPartSizedInflate').
I don't insist on Abbrevia if there is any other better/simple solution.
Thank you!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnUnzipClick(Sender: TObject);
  2. var
  3.   UnZipper: TAbUnZipper;
  4. begin  
  5.   UnZipper := TAbUnZipper.Create(nil);
  6.   UnZipper.BaseDirectory := '/path/to/folder';  
  7.  
  8.   try
  9.      UnZipper.FileName := 'gzfile.gz';
  10.      UnZipper.ExtractFiles('*.csv');
  11.   finally
  12.      UnZipper.Free;
  13.   end;
  14. end;
  15.  

Handoko

  • Hero Member
  • *****
  • Posts: 4687
  • My goal: build my own game engine using Lazarus
Re: Decompressing a gz file
« Reply #1 on: July 02, 2022, 07:32:23 pm »

justnewbie

  • Sr. Member
  • ****
  • Posts: 278
Re: Decompressing a gz file
« Reply #2 on: July 02, 2022, 07:39:59 pm »
Have you tried this?

https://wiki.freepascal.org/paszlib
Thanks for the answer! Does it support 'gz'?

justnewbie

  • Sr. Member
  • ****
  • Posts: 278
Re: Decompressing a gz file
« Reply #3 on: July 02, 2022, 07:49:33 pm »
Have you tried this?

https://wiki.freepascal.org/paszlib

I got "corrupt zip file" error (not corrupted!), so I think it doesn't support 'gz'.

Handoko

  • Hero Member
  • *****
  • Posts: 4687
  • My goal: build my own game engine using Lazarus
Re: Decompressing a gz file
« Reply #4 on: July 02, 2022, 09:25:21 pm »
Near the bottom in link I gave you, it said:

Quote
Article demonstrating handling tar, bzip2, gzip, zip files ...

I haven't tried, but maybe you can find your answer in that article.

dsiders

  • Hero Member
  • *****
  • Posts: 673
Re: Decompressing a gz file
« Reply #5 on: July 02, 2022, 09:31:25 pm »
Hi guys,
I have a *.gz file (a compressed *.csv) and I need to decompress it.

paszlib  includes the file gzio.pas which provides gzip support. It also includes an example in source/packages/paszlib/examples/minigzip.pas.

[Edit]

In fpc...
« Last Edit: July 02, 2022, 09:33:36 pm by dsiders »
Preview Lazarus 2.3.0 documentation at: https://dsiders.gitlab.io/lazdocsnext

justnewbie

  • Sr. Member
  • ****
  • Posts: 278
Re: Decompressing a gz file
« Reply #6 on: July 02, 2022, 10:19:13 pm »
Hi guys,
I have a *.gz file (a compressed *.csv) and I need to decompress it.

paszlib  includes the file gzio.pas which provides gzip support. It also includes an example in source/packages/paszlib/examples/minigzip.pas.

[Edit]

In fpc...

Strange. I got "corrupt zip file" message by using the code below. The file is surely not corrupted.
Unfortunately, the minigzip.pas is too difficult for my low skills.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnUnzipClick(Sender: TObject);
  2. var  
  3.   UnZipper: TUnZipper;  
  4. begin
  5.  
  6.   UnZipper := TUnZipper.Create;
  7.   try
  8.     UnZipper.FileName := 'gzfile.gz';
  9.     UnZipper.OutputPath := '/path/';
  10.     UnZipper.Examine;
  11.     UnZipper.UnZipAllFiles;
  12.  
  13.   finally
  14.     UnZipper.Free;
  15.   end;
  16.  
  17. end;      
  18.  


y.ivanov

  • Sr. Member
  • ****
  • Posts: 442
Re: Decompressing a gz file
« Reply #7 on: July 03, 2022, 12:40:49 am »
Here is uncompress from minigzip.pas extracted. Hope it is simple enough.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$I-}
  4.  
  5. uses
  6.   gzio, SysUtils;
  7.  
  8. const
  9.   BUF_LEN = 16 * 1024;
  10.  
  11.   procedure gzUncompress(infile: gzFile; var outFile: file);
  12.   var
  13.     len, written: LongInt;
  14.     err: SmallInt;
  15.     buf: array of Byte;
  16.   begin
  17.     SetLength(buf, BUF_LEN);
  18.     while True do
  19.     begin
  20.       len := gzread(infile, @buf[0], BUF_LEN);
  21.       if len < 0 then
  22.         raise Exception.Create(gzerror(infile, err));
  23.       if len = 0 then
  24.         Break;
  25.       BlockWrite(outFile, buf[0], len, written);
  26.       if written <> len then
  27.         raise Exception.Create('write error');
  28.     end;
  29.   end;
  30.  
  31. var
  32.   inFile: gzFile;
  33.   outFile: File;
  34.   inName, outName: String;
  35. begin
  36.   inName := ParamStr(1);
  37.   outName:= ParamStr(2);
  38.  
  39.   if (inName = '') or (outName = '') then
  40.   begin
  41.     WriteLn('usage: ', ParamStr(0), ' input_file.gz output_file');
  42.     Halt(1);
  43.   end;
  44.  
  45.   inFile := gzopen(inName, 'r');
  46.   if inFile = Nil then
  47.   begin
  48.     WriteLn('Can''t gzopen ', inName);
  49.     Halt(1);
  50.   end;
  51.  
  52.   Assign(outFile, outname);
  53.   Rewrite(outfile, 1);
  54.   if IOResult <> 0 then
  55.   begin
  56.     WriteLn('open error: ', IOResult);
  57.     Halt(1);
  58.   end;
  59.  
  60.   try
  61.     gzUncompress(inFile, outFile);
  62.   except
  63.     on E: Exception do
  64.       WriteLn(E.Message);
  65.   end;
  66.  
  67.   Close(outFile);
  68.   gzclose(inFile)
  69. end.
  70.  
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

justnewbie

  • Sr. Member
  • ****
  • Posts: 278
Re: Decompressing a gz file
« Reply #8 on: July 03, 2022, 03:20:47 pm »
Thank you y.ivanov.
Unfortunately, I don't know how to implement this program into my application.
(It worked running as a program, gzip file was decompressed successfully.)
« Last Edit: July 03, 2022, 03:26:37 pm by justnewbie »

 

TinyPortal © 2005-2018