hvis det kun er filen navn og ikke en længere path som bliver læst ind i s kan du bare sammenligne 'test.dat' med s ellers i stedet for AnsiEndsStr i det følgende.
if(AnsiEndsStr('test.dat',s))then
begin
s := dest + s; //include the path
outfile := TFileStream.Create(s, fmCreate);
decompr := TDecompressionStream.Create(infile);
try
outfile.CopyFrom(decompr, l);
finally
outfile.Free;
decompr.Free;
end;
end;
Nej det virker ikke.
Jeg har brugt denne kode til at komprimere filerne til en fil.
procedure CompressFiles(Files: TStrings; const Filename: string);
var
infile, outfile, tmpFile: TFileStream;
compr: TCompressionStream;
i, l: Integer;
s: string;
begin
if Files.Count > 0 then
begin
outFile := TFileStream.Create(Filename, fmCreate);
try
{ the number of files }
l := Files.Count;
outfile.Write(l, SizeOf(l));
for i := 0 to Files.Count - 1 do
begin
infile := TFileStream.Create(Files[i], fmOpenRead);
try
{ the original filename }
s := ExtractFilename(Files[i]);
l := Length(s);
outfile.Write(l, SizeOf(l));
outfile.Write(s[1], l);
{ the original filesize }
l := infile.Size;
outfile.Write(l, SizeOf(l));
{ compress and store the file temporary}
tmpFile := TFileStream.Create('tmp', fmCreate);
compr := TCompressionStream.Create(clMax, tmpfile);
try
compr.CopyFrom(infile, l);
finally
compr.Free;
tmpFile.Free;
end;
{ append the compressed file to the destination file }
tmpFile := TFileStream.Create('tmp', fmOpenRead);
try
outfile.CopyFrom(tmpFile, 0);
finally
tmpFile.Free;
end;
finally
infile.Free;
end;
end;
finally
outfile.Free;
end;
DeleteFile('tmp');
end;
end;