![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Модуль Compress.pas - распаковка ресурсов в файл
koha © (2007-01-08 02:23:00)Вот на скорую руку модуль состряпал для запаковки / файлов на основе Zlib, так же для Распаковки Встроенных ресурсов в файл.
Кто, что подскажет для лучшей работы и исправления ляпусов ? А так вроде работает.
unit Compress;
interface
Uses Classes, Windows, SysUtils, ZLib;
Function CompressFile(InpFile: String; OutFile: String; N: Integer): boolean;
Function DecompressFile(InpFile: String; OutFile: String): boolean;
Function DecompressResToFile(ResName: String; FileName: String): Boolean;
Var
INP_STRM, OUT_STRM : TFileStream;
RES_STRM : TResourceStream;
COMPRESS_STRM : TCompressionStream;
DECOMPRESS_STRM : TDecompressionStream;
implementation
Function CompressFile(InpFile: String; OutFile: String; N: Integer): boolean;
Var
FBuffer: Array[0..1023] of char;
ByteRaed: Integer;
Begin
try
try
INP_STRM := TFileStream.Create(InpFile,fmOpenRead);
OUT_STRM := TFileStream.Create(OutFile,fmCreate);
COMPRESS_STRM := TCompressionStream.Create(clMax,OUT_STRM);
repeat
ByteRaed := INP_STRM.Read(FBuffer,SizeOf(FBuffer));
COMPRESS_STRM.Write(FBuffer,ByteRaed);
until ByteRaed < 1024;
Result:=True;
finally
COMPRESS_STRM.Free;
INP_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do FBuffer[ByteRaed]:=#0;
end;
Except
Result:=False;
COMPRESS_STRM.Free;
INP_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do FBuffer[ByteRaed]:=#0;
end;
end;
Function DecompressFile(InpFile: String; OutFile: String): boolean;
Var
Buffer: Array[0..1023] of char;
ByteRaed: Integer;
Begin
try
try
INP_STRM := TFileStream.Create(InpFile,fmOpenRead);
OUT_STRM := TFileStream.Create(OutFile,fmCreate);
DECOMPRESS_STRM := TDecompressionStream.Create(OUT_STRM);
repeat
ByteRaed := INP_STRM.Read(Buffer,SizeOf(Buffer));
DECOMPRESS_STRM.Write(Buffer,ByteRaed);
until ByteRaed < 1024;
Result:=True;
finally
DECOMPRESS_STRM.Free;
INP_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do Buffer[ByteRaed]:=#0;
end;
Except
Result:=False;
DECOMPRESS_STRM.Free;
INP_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do Buffer[ByteRaed]:=#0;
end;
end;
Function DecompressResToFile(ResName: String; FileName: String): Boolean;
Var
Buffer: Array[0..1023] of char;
ByteRaed: Integer;
Begin
Try
Try
RES_STRM := TResourceStream.Create(HInstance,ResName,RT_RCDATA);
OUT_STRM := TFileStream.Create(FileName,fmCreate);
DECOMPRESS_STRM := TDecompressionStream.Create(RES_STRM);
repeat
ByteRaed:=DECOMPRESS_STRM.Read(Buffer,SizeOf(Buffer));
OUT_STRM.Write(Buffer,ByteRaed);
until ByteRaed < 1024;
Result:=true;
finally
DECOMPRESS_STRM.Free;
RES_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do Buffer[ByteRaed]:=#0;
end;
except
DECOMPRESS_STRM.Free;
RES_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do Buffer[ByteRaed]:=#0;
Result:=false;
end;
end;
end.
Германн (2007-01-08 02:43:00) [21]Я тоже взглянул только мельком. Но моё имхо - неверное вложение блоков. Блок exceptнужно вкладывать в блок finally. А не наоборот. Тогда и не будет желания делать то, что согласно [2] нужно убрать.
Anatoly Podgoretsky (2007-01-08 02:29:00) [22]> koha (08.01.2007 02:23:00) [0]
Убрать из except данные строки
DECOMPRESS_STRM.Free;
RES_STRM.Free;
OUT_STRM.Free;
for ByteRaed:=0 to 1023 do Buffer[ByteRaed]:=#0;
Anatoly Podgoretsky (2007-01-08 02:28:00) [23]> koha (08.01.2007 02:23:00) [0]
Глубоко не смотрел, но один ляпсус налицо, при ошибке будет попытка повторного освобождения объектов и как результат AV