unit ssZip;
interface
uses
Classes;
procedure CompressStream(inStream, outStream: TStream);
function DecompressStream(inStream, outStream: TStream): Boolean;
procedure CopyFileCompress(FileNameFrom, FileNameTo: PWideChar);
procedure CopyFileDecompress(FileNameFrom, FileNameTo: PWideChar);
implementation
uses
ZLib, Windows, SysUtils;
const
BUFSIZE = 1024 * 16;
procedure CompressStream(inStream, outStream: TStream);
var
cs: TCompressionStream;
begin
inStream.Position := 0;
outStream.Size := 0;
cs := TCompressionStream.Create(clDefault, outStream);
cs.CopyFrom(inStream, 0);
// copy all
cs.Free;
end;
function DecompressStream(inStream, outStream: TStream): Boolean;
var
ds: TDecompressionStream;
buf:
string;
begin
Result := True;
try
inStream.Position := 0;
outStream.Size := 0;
ds := TDecompressionStream.Create(inStream);
repeat
SetLength(buf, BUFSIZE);
SetLength(buf, ds.
Read(buf[1], BUFSIZE));
outStream.
Write(buf[1], Length(buf));
until buf = '
';
except
Result := False;
end;
ds.Free;
end;
procedure CopyFileCompress(FileNameFrom, FileNameTo: PWideChar);
var
FSC, FSD: TFileStream;
begin
FSD := TFileStream.Create(
String(FileNameFrom), fmOpenRead);
FSC := TFileStream.Create(
String(FileNameTo), fmCreate);
CompressStream(FSD, FSC);
FreeAndNil(FSD);
FreeAndNil(FSC);
end;
procedure CopyFileDecompress(FileNameFrom, FileNameTo: PWideChar);
var
FSC, FSD: TFileStream;
EF: Boolean;
begin
FSC := TFileStream.Create(
String(FileNameFrom), fmOpenRead);
FSD := TFileStream.Create(
String(FileNameTo), fmCreate);
EF :=
not DecompressStream(FSC, FSD);
FreeAndNil(FSC);
FreeAndNil(FSD);
if EF
then
CopyFile(FileNameFrom, FileNameTo, False);
end;
end.