diff --git a/.gitattributes b/.gitattributes index 8fddcdbcf9..9208943f39 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1030,6 +1030,7 @@ packages/bzip2/LICENSE svneol=native#text/plain packages/bzip2/Makefile svneol=native#text/plain packages/bzip2/Makefile.fpc svneol=native#text/plain packages/bzip2/Makefile.fpc.fpcmake svneol=native#text/plain +packages/bzip2/examples/pasbunzip2.pas svneol=native#text/plain packages/bzip2/examples/pasbzip.pas svneol=native#text/plain packages/bzip2/fpmake.pp svneol=native#text/plain packages/bzip2/src/bzip2.pas svneol=native#text/plain diff --git a/packages/bzip2/examples/pasbunzip2.pas b/packages/bzip2/examples/pasbunzip2.pas new file mode 100644 index 0000000000..9893eefaef --- /dev/null +++ b/packages/bzip2/examples/pasbunzip2.pas @@ -0,0 +1,86 @@ +program pasbunzip2; +{ + This file is part of the Free Pascal packages. + Copyright (c) 2012 Reinier Olislagers + + Tests bzip2 decompression. + + See the file COPYING.FPC, included in this distribution, + for details about the copyright. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + **********************************************************************} +{$mode objfpc}{$H+} + +{ +Example .bz2 file extractor. +Decompresses a .bz2 file into another file using the classes-based bzip2stream unit +Usage: bunzip2 compressedfile.bz2 +} + +uses classes, bzip2stream; + +function Decompress(SourceFile, TargetFile: string): boolean; +var + InFile:TFileStream; + Decompressed:TDecompressBzip2Stream; + OutFile:TFileStream; + Buffer: Pointer; + i: integer; +const buffersize=$2000; +begin + result:=false; //fail by default + InFile:=TFileStream.Create(SourceFile, fmOpenRead); + try + try + Decompressed:=TDecompressBzip2Stream.Create(InFile); + except + // So[5mething went wrong, e.g. invalid format + // Now get out of function with result false + exit; + end; + OutFile:=TFileStream.Create(TargetFile, fmCreate); + try + //We don't have seek on the TDecompressBzip2stream, so can't use + //CopyFrom... + //Decompressed.CopyFrom(InFile, InFile.Size); + GetMem(Buffer,BufferSize); + repeat + i:=Decompressed.Read(buffer^,BufferSize); + if i>0 then + OutFile.WriteBuffer(buffer^,i); + until i'); + halt(13); // show error in exit code + end; + DecompressedFile:=CompressedFile+'.out'; + if Decompress(CompressedFile, DecompressedFile) then + begin + writeln('Decompressed '+CompressedFile+' to '+DecompressedFile); + end + else + begin + writeln('An error occurred while decompressing '+CompressedFile); + halt(1); // show error in exit code + end; +end.