Difference between revisions of "bzip2lib"

From Lazarus wiki
Jump to navigationJump to search
m
 
(11 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
=== About ===
 
=== About ===
bzip2lib provides stream classes for bzip2 compression and decompression.
+
bzip2lib provides stream classes for bzip2 compression and decompression. It uses external bzip2 libraries.
  
Unit bzip2lib contains 2 stream classes:
+
Unit bzip2lib contains 2 streams:
 
* TBzip2CompressStream for compressing (packing) data to bzip2 format.
 
* TBzip2CompressStream for compressing (packing) data to bzip2 format.
 
* TBzip2DecompressStream for decompressing (unpacking) data back to its original form.
 
* TBzip2DecompressStream for decompressing (unpacking) data back to its original form.
  
Unit bzip2lib is collected and edited from 2 unit files in Jcl package:
+
Unit bzip2lib is collected and edited from 2 unit files in [https://wiki.delphi-jedi.org/wiki/JEDI_Code_Library Jcl] package:
 
* JclCompression  
 
* JclCompression  
 
* bzip2
 
* bzip2
Line 19: Line 19:
 
Juha Manninen <juha dot manninen (at) phnet dot fi>
 
Juha Manninen <juha dot manninen (at) phnet dot fi>
  
+ the original authors from Jcl code.
+
+ the original authors of Jcl code.
  
 
=== License ===
 
=== License ===
Line 25: Line 25:
  
 
=== Download ===
 
=== Download ===
The latest stable release can be found on http://sourceforge.net/projects/lazarus-ccr/files/bzip2lib/bzip2lib.tar.gz/download.
+
The latest stable release can be found on http://sourceforge.net/projects/lazarus-ccr/files/bzip2lib/bzip2lib-1.tar.gz/download.
  
 
=== Change Log ===
 
=== Change Log ===
Line 32: Line 32:
 
=== Dependencies / System Requirements ===
 
=== Dependencies / System Requirements ===
 
The code uses external bzip2 libraries for its job:
 
The code uses external bzip2 libraries for its job:
  'bzip2.dll' in Windows
+
* 'bzip2.dll' in Windows
  'libbz2.so.1' in Linux
+
* 'libbz2.so.1' in Linux
  
 
Status: Stable
 
Status: Stable
  
Issues: The author has tested the code only under Linux. There is works well.
+
Issues: The author has tested the code only under Linux. There it works well.
  
 
=== Usage ===
 
=== Usage ===
Just include unit bzip2lib in the "uses" section.
+
Just include unit <code>bzip2lib</code> in the "uses" section. Set path for it in project settings if needed.
External bzip2 library must be installed in your machine (see *Dependencies*).
+
The external bzip2 library must be installed in your machine (see *Dependencies*).
Use TBzip2CompressStream and TBzip2DecompressStream just like any stream,
+
Use <code>TBzip2CompressStream</code> and <code>TBzip2DecompressStream</code> just like any stream, connecting them to file stream or memory stream if needed.
connecting them to file stream or memory stream if needed.
+
 
 +
Note: there is a bzip2 component supplied with FPC: packages/bzip2.
 +
It is a pure Pascal solution (doesn't need external libraries) but it can only decompress.
 +
If you also need compression then bzip2lib is for you.
  
 
=== Example code for bzip2lib ===
 
=== Example code for bzip2lib ===
 +
 
* Compressing a string to a file. "S" is the string to compress.
 
* Compressing a string to a file. "S" is the string to compress.
  
<Delphi>
+
<syntaxhighlight lang="pascal">
  OutFileStream := TFileStream.Create(CompressedFileName, fmCreate);
+
OutFileStream := TFileStream.Create(CompressedFileName, fmCreate);
  CompressStream := TBzip2CompressStream.Create(OutFileStream);
+
CompressStream := TBzip2CompressStream.Create(OutFileStream);
  try
+
try
    CompressStream.Write(Pointer(S)^, Length(S));
+
  CompressStream.Write(Pointer(S)^, Length(S));
  finally
+
finally
    CompressStream.Free;
+
  CompressStream.Free;
    OutFileStream.Free;
+
  OutFileStream.Free;
  end;
+
end;</syntaxhighlight>
</Delphi>
 
  
 
* Decompressing from a file to a TStringList (SL). TStringList.LoadFromStream can be used here.
 
* Decompressing from a file to a TStringList (SL). TStringList.LoadFromStream can be used here.
  
<Delphi>
+
<syntaxhighlight lang="pascal">
  InFileStream := TFileStream.Create(CompressedFileName, fmOpenRead);
+
SL := TStringList.Create;
  DecompressStream := TBzip2DecompressStream.Create(InFileStream);
+
InFileStream := TFileStream.Create(CompressedFileName, fmOpenRead);
  try
+
DecompressStream := TBzip2DecompressStream.Create(InFileStream);
    SL.Clear;
+
try
    SL.LoadFromStream(DecompressStream);
+
  SL.LoadFromStream(DecompressStream);
    SL.SaveToFile(OutFileName);
+
  SL.SaveToFile(OutFileName);
  finally
+
finally
    DecompressStream.Free;
+
  DecompressStream.Free;
    InFileStream.Free;
+
  InFileStream.Free;
   end;
+
   SL.Free;
</Delphi>
+
end;</syntaxhighlight>
 +
 
 +
[[Category:Components]]
 +
[[Category:Lazarus-CCR]]

Latest revision as of 15:34, 29 July 2020

About

bzip2lib provides stream classes for bzip2 compression and decompression. It uses external bzip2 libraries.

Unit bzip2lib contains 2 streams:

  • TBzip2CompressStream for compressing (packing) data to bzip2 format.
  • TBzip2DecompressStream for decompressing (unpacking) data back to its original form.

Unit bzip2lib is collected and edited from 2 unit files in Jcl package:

  • JclCompression
  • bzip2

The purpose was to make it compile and work with FPC (Free Pascal Compiler). In the process lots of code with conditional compilation directives was removed and also many unit dependencies were removed.

The download contains the source unit file and a demo program which compresses and then again decompresses a text file given as a command line parameter.

Author

Juha Manninen <juha dot manninen (at) phnet dot fi>

+ the original authors of Jcl code.

License

This code is licensed under the same terms as Jcl library.

Download

The latest stable release can be found on http://sourceforge.net/projects/lazarus-ccr/files/bzip2lib/bzip2lib-1.tar.gz/download.

Change Log

  • Version 1.0, Date: 2009.11.06

Dependencies / System Requirements

The code uses external bzip2 libraries for its job:

  • 'bzip2.dll' in Windows
  • 'libbz2.so.1' in Linux

Status: Stable

Issues: The author has tested the code only under Linux. There it works well.

Usage

Just include unit bzip2lib in the "uses" section. Set path for it in project settings if needed. The external bzip2 library must be installed in your machine (see *Dependencies*). Use TBzip2CompressStream and TBzip2DecompressStream just like any stream, connecting them to file stream or memory stream if needed.

Note: there is a bzip2 component supplied with FPC: packages/bzip2. It is a pure Pascal solution (doesn't need external libraries) but it can only decompress. If you also need compression then bzip2lib is for you.

Example code for bzip2lib

  • Compressing a string to a file. "S" is the string to compress.
OutFileStream := TFileStream.Create(CompressedFileName, fmCreate);
CompressStream := TBzip2CompressStream.Create(OutFileStream);
try
  CompressStream.Write(Pointer(S)^, Length(S));
finally
  CompressStream.Free;
  OutFileStream.Free;
end;
  • Decompressing from a file to a TStringList (SL). TStringList.LoadFromStream can be used here.
SL := TStringList.Create;
InFileStream := TFileStream.Create(CompressedFileName, fmOpenRead);
DecompressStream := TBzip2DecompressStream.Create(InFileStream);
try
  SL.LoadFromStream(DecompressStream);
  SL.SaveToFile(OutFileName);
finally
  DecompressStream.Free;
  InFileStream.Free;
  SL.Free;
end;