Autor Beitrag
Mike_C
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: So 27.10.02 12:46 
Hi!

ich möchte zum Verschlüsseln von String-Daten die Unit ZLIB verwenden. Diese Unit (ist bei delphi 7 dabei) unterstützt die ZIP-Verschlüsselung.
In eine Datei schreiben ist kein Problem:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
procedure TForm1.btnZIPClick(Sender: TObject);
var ZIPStream : TCompressionStream;
    DestStream : TMemoryStream;
    SrcStream : TMemoryStream;
    Buffer: string;
    i: integer;
begin
  DestStream := TMemoryStream.Create; //Hier wird rein geschrieben!!!

  ZipStream := TCompressionStream.Create(clDefault, DestStream);  //Hier wird verschlüsselt!!!

  SrcStream := TMemoryStream.Create;  //Hier wird draus gelesen!!!
  Memo1.Lines.SaveToStream(SrcStream);
  for i := 0 to Memo1.Lines.Count-1 do begin
    SrcStream.Seek(0,soFromCurrent);
    SrcStream.Read(Buffer,1000);
    ZipStream.Write(Buffer,1000);
  end;
  SrcStream.Free;

  ZipStream.Free;

  DestStream.SaveToFile(ExtractFilePath(Application.Exename)+'Test.file');
  DestStream.Free;
end;


Nur lesen geht noch nicht:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.btnUnZIPClick(Sender: TObject);
var ZIPStream: TDecompressionStream;
    SrcStream: TMemoryStream;
    Buffer,s: string;
begin
  SrcStream := TMemoryStream.Create;
  SrcStream.LoadFromFile(ExtractFilePath(Application.Exename)+'Test.file');
  ZIPStream := TDecompressionStream.Create(SrcStream);

  ZIPStream.Seek(0,soFromBeginning);
  ZIPStream.Read(Buffer,1000);
  s := s+Buffer;

  ZIPStream.Free;
  SrcStream.Free;
  Memo1.Text := s;
end;


Kann mir jemand helfen?
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 27.10.02 19:18 
probier' mal beim lesen die Variable Buffer als String fester Länge zu definieren. Das geht einfach mit Buffer: String[10] (oder eine andere Zahl unter 255). Dann kannst du natürlich nur diese Zahl an Zeichen auf einmal lesen, aber wenn du 'ne Schleife machst, in der du solange 10 Byte ausliest, bis du am Ende bist, dürfte auch das kein Problem sein. Diese Einschränkung hatte ich mal in eine BZip"-Unit, die laut Autor genauso arbeiten soll wie ZLIB. Ob's auch da genauso ist, weiß ich nicht, probier's aber mal aus.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: So 27.10.02 19:42 
tommie-lie hat folgendes geschrieben:
probier' mal beim lesen die Variable Buffer als String fester Länge zu definieren. Das geht einfach mit Buffer: String[10] (oder eine andere Zahl unter 255). Dann kannst du natürlich nur diese Zahl an Zeichen auf einmal lesen, aber wenn du 'ne Schleife machst, in der du solange 10 Byte ausliest, bis du am Ende bist...


Laut Help zu ZLIB gibt mir die Property POSITION von jedem TDecompressionstream oder TCompressionstream die Anzahl der Bytes zurück, die bereits gelesen wurden. Nur wenn ich abfrage
ausblenden Quelltext
1:
2:
3:
while ZIPStream.Position > 0 do begin
...
end

wird die while-schleife einfach übersprungen. Wie bekomme ich also raus, wann ich am ende vom Stream bin? :?:
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 27.10.02 19:45 
Gibt's kein Stream.EndOfFile?

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: So 27.10.02 19:48 
nicht, das ich wüsste.
Damit wär's natürlich einfach

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: So 27.10.02 20:07 
Ich hab's schonmal geschafft was aus der Datei zu lesen.
Allerdings ist das Ergebnis nicht das was ich mit gewünscht habe.

Der Input war "Hallo", der Output war "Ówt÷"

Der Code für die Lesen-Routine sieht jetzt so aus:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
procedure TForm1.btnUnZIPClick(Sender: TObject);
var ZIPStream: TDecompressionStream;
    SrcStream: TMemoryStream;
    Buffer: string[200];
    s: string;

    size : integer;
begin
  SrcStream := TMemoryStream.Create;
  SrcStream.LoadFromFile(ExtractFilePath(Application.Exename)+'Test.file');
  ZIPStream := TDecompressionStream.Create(SrcStream);

  Size := SrcStream.Size;

  ZIPStream.Seek(0,soFromBeginning);

  s := '';
  while size > 0 do begin
    Buffer := '';
    ZIPStream.Read(Buffer,200);
    s := s+Buffer;
    dec(size,200);
    ZipStream.Seek(0,soFromCurrent);
  end;

  ZIPStream.Free;
  SrcStream.Free;
  Memo1.Text := s;
end;


Warum funzt das nicht??? :?: :? :(
Hat wer eine :idea: ?

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 27.10.02 20:38 
Sorry, keine Ahnung.

Sieht aus, als würde er's nicht entpacken sondern einfach so lesen...
Außerdem ist auch ein Zei chen zuviel. Im Output sind's 6 statt 5

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
Mike_C Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 207

Win XP
D7 Enterprise
BeitragVerfasst: So 27.10.02 21:01 
@tommie: schade. aber thx alot für deine hilfe

_________________
Life is, what some people call a mystery. To me life's just a lesson, you're learning when you're through. So why do we try to understand?