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