Einzelnen Beitrag anzeigen

NickelM

Registriert seit: 22. Jul 2007
Ort: Carlsberg
445 Beiträge
 
Delphi 2009 Professional
 
#3

AW: Klasse mit vielen MemoryStreams (andere Möglichkeit)

  Alt 2. Jan 2011, 13:48
hmm okay also ich poste mal wie ich das gemacht habe. Vllt hilft es ja weiter...

Delphi-Quellcode:
type
  TZLibEntpackedStream = class(TMemoryStream) //Da TMemoryStream von TObject bereits abstammt
  private
  FFileName : TFileName;
  public
  property FileName : TFileName read FFileName write FFileName;
  end;

  TStreamList = class(TObjectList)
  protected
  function GetItem(Index: Integer): TZLibEntpackedStream;
  procedure SetItem(Index: Integer; AStream: TZLibEntpackedStream);
  public
  function Add(AStream: TZLibEntpackedStream): Integer;
  function Remove(AStream: TZLibEntpackedStream): Integer;
  function IndexOf(AStream: TZLibEntpackedStream): Integer;
  function First: TZLibEntpackedStream;
  function Last: TZLibEntpackedStream;
  procedure Insert(Index: Integer; AStream: TZLibEntpackedStream);
  property Items[Index: Integer]: TZLibEntpackedStream read GetItem write SetItem; default;
  end;

  TStreamZlibMultiple = class(TJvZlibMultiple)
  public
  procedure DecompressStream(Stream: TStream; var StreamList : TStreamList); overload;
  end;

implementation
function TStreamList.GetItem(Index: Integer): TZLibEntpackedStream;
begin
Result := TZLibEntpackedStream(inherited GetItem(Index));
end;

procedure TStreamList.SetItem(Index: Integer; AStream: TZLibEntpackedStream);
begin
inherited Items[Index] := AStream;
end;

function TStreamList.Add(AStream: TZLibEntpackedStream) : Integer;
begin
Result := inherited Add(AStream);
end;

function TStreamList.Remove(AStream: TZLibEntpackedStream): Integer;
begin
Result := inherited Remove(AStream);
end;

function TStreamList.IndexOf(AStream: TZLibEntpackedStream): Integer;
begin
Result := inherited IndexOf(AStream);
end;

function TStreamList.First: TZLibEntpackedStream;
begin
Result := TZLibEntpackedStream(inherited First);
end;

function TStreamList.Last: TZLibEntpackedStream;
begin
Result := TZLibEntpackedStream(inherited Last);
end;

procedure TStreamList.Insert(Index: Integer; AStream: TZLibEntpackedStream);
begin
inherited Insert(Index,AStream);
end;

procedure TStreamZlibMultiple.DecompressStream(Stream: TStream; var StreamList : TStreamList);
var
  MemoryStream: TZLibEntpackedStream;
  ZStream: TJclZLibDecompressStream;
  CStream: TMemoryStream;
  B, LastPos: Byte;
  AnsiS: AnsiString;
  S: string;
  Count, FileSize, I: Integer;
  Buffer: array [0..1023] of Byte;
  TotalByteCount: Longword;
  FileStreamSize, StreamSize: Int64;
begin
  if not Assigned(StreamList) then
  StreamList := TStreamList.Create;

  StreamSize := Stream.Size; // cache, to not FileSeek on every iteration
  while Stream.Position < StreamSize do
  begin
    //Read and force the directory //Fällt weg weil ich das net brauch muss aber Count gelesen werden
    Stream.Read(B, SizeOf(B));

    S := '';

    //Read filename
    Stream.Read(B, SizeOf(B));
    if B > 0 then
    begin
      AnsiS := AnsiString(S);
      LastPos := Length(AnsiS);
      SetLength(AnsiS, LastPos + B);
      Stream.Read(AnsiS[LastPos + 1], B);
      S := string(AnsiS);
    end;

    Stream.Read(FileSize, SizeOf(FileSize));
    Stream.Read(I, SizeOf(I));
    CStream := TMemoryStream.Create;

    try
      CStream.CopyFrom(Stream, I);
      CStream.Position := 0;
        //Decompress the file

        //1. Funkt net
        MemoryStream := StreamList.Items[StreamList.Add(TZLibEntpackedStream.Create)]; //Hier kommt der Fehler
        MemoryStream.FFileName := S;

        //2. Funkt net
        MemoryStream := TZLibEntpackedStream.Create;
        StreamList.Add(MemoryStream);//Hier kommt der Fehler
        MemoryStream.FFileName := S;

        ZStream := TJclZLibDecompressStream.Create(CStream);
        try
          TotalByteCount := 0;

          { (RB) ZStream has an OnProgress event, thus copyfrom can be used }
          FileStreamSize := 0;
          repeat
            Count := ZStream.Read(Buffer, SizeOf(Buffer));
            if Assigned(MemoryStream) then
            begin
              Inc(FileStreamSize, MemoryStream.Write(Buffer, Count));
              DoProgress(FileStreamSize, FileSize);
            end;
            Inc(TotalByteCount, Count);
          until Count = 0;
          if Assigned(OnDecompressedFile) then
            OnDecompressedFile(Self, S, TotalByteCount);
        finally
          FreeAndNil(MemoryStream);
          ZStream.Free;
        end;
    finally
      CStream.Free;
    end;
  end;
end;

Für den Original-Code kann man ja selber gugen.
Also ich hab keine Ahnung woran das liegt.

Aufrufen tuh ich das so:
TStreamZlibMultiple(JvZlibMultiple1).DecompressStream(Stream,StreamList); Stream natürlich initalisiert und auch eine StreamList Variable erstellen xD
Nickel
"Lebe und denke nicht an morgen"
Zitat aus dem gleichnamigen Bollywoodfilm.
  Mit Zitat antworten Zitat