unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MD5;
type
tPackFile =
record
Version : Byte;
Password :
String[32];
Size : Integer;
Package :
array of Byte;
end;
type
TForm1 =
class(TForm)
btnOpen: TButton;
OpenDialog: TOpenDialog;
edtFile: TEdit;
btnCompress: TButton;
SaveDialog: TSaveDialog;
btnDecompress: TButton;
procedure btnOpenClick(Sender: TObject);
procedure btnCompressClick(Sender: TObject);
procedure btnDecompressClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
NewFile : tPackFile;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnOpenClick(Sender: TObject);
begin
//Welche Datei soll gepackt werden?
if OpenDialog.Execute
then edtFile.Text := OpenDialog.FileName;
end;
procedure TForm1.btnCompressClick(Sender: TObject);
var
FStream : TFileStream;
FHandle :
file of byte;
cTmp : byte;
ByteIdx : Integer;
begin
if SaveDialog.Execute
then begin
//init
NewFile.Version := 1;
NewFile.Password := MD5Print(MD5File(OpenDialog.FileName));
SetLength(NewFile.
Package, 1000000);
ByteIdx := 0;
//Datei die gepackt werden soll, öffnen und einlesen...
System.Assign(FHandle, edtFile.Text);
{$I-}
Reset(FHandle);
{$I+}
if IoResult = 0
then begin
while not Eof(FHandle)
do begin
//Zeichen lesen
Read(FHandle, cTmp);
NewFile.
Package[ByteIdx] := cTmp;
Inc(ByteIdx);
end;
{while}
System.Close(FHandle);
end;
//Größe der Datei
SetLength(NewFile.
Package, ByteIdx);
NewFile.Size := Length(NewFile.
Package);
//Datei speichern...
FStream := TFileStream.Create(SaveDialog.FileName, fmCreate
or fmShareExclusive);
try
FStream.WriteBuffer(NewFile.Version, SizeOf(NewFile.Version));
FStream.WriteBuffer(NewFile.Password, SizeOf(NewFile.Password));
FStream.WriteBuffer(NewFile.Size, SizeOf(NewFile.Size));
for ByteIdx := 0
to High(NewFile.
Package)
do begin
FStream.WriteBuffer(NewFile.
Package[ByteIdx], SizeOf(NewFile.
Package[ByteIdx]));
end;
{for}
finally
FStream.Free;
end;
end;
{if}
end;
procedure TForm1.btnDecompressClick(Sender: TObject);
var
FStream : TFileStream;
Index : Integer;
FHandle :
file of byte;
begin
if SaveDialog.Execute
then begin
//init
NewFile.Version := 0;
NewFile.Password := '
';
NewFile.Size := 0;
NewFile.
Package :=
nil;
//Datei öffnen und einlesen...
FStream := TFileStream.Create(OpenDialog.FileName, fmOpenRead
or fmShareExclusive);
try
FStream.ReadBuffer(NewFile.Version, SizeOf(NewFile.Version));
FStream.ReadBuffer(NewFile.Password, SizeOf(NewFile.Password));
if NewFile.Password = '
3b2e0b0079c60f165ce92eb0bce18b5d'
then begin
FStream.ReadBuffer(NewFile.Size, SizeOf(NewFile.Size));
SetLength(NewFile.
Package, NewFile.Size);
for Index := 0
to NewFile.Size-1
do begin
FStream.ReadBuffer(NewFile.
Package[
Index], SizeOf(NewFile.
Package[
Index]));
end;
{for}
end
else
ShowMessage('
File damaged');
//Datei erstellen...
System.Assign(FHandle, SaveDialog.FileName);
{$I-}
Rewrite(FHandle);
{$I+}
if IoResult = 0
then begin
for Index := 0
to High(NewFile.
Package)
do begin
Write(FHandle, NewFile.
Package[
Index]);
end;
System.Close(FHandle);
end;
{if}
finally
FStream.Free;
end;
end;
{if SaveDialog}
end;
end.