Delphi LZMA Decompression sample

后端 未结 1 1182
你的背包
你的背包 2021-01-28 22:35

I found in this thread link of the delphi-zip library that has implementation of LZMA. But I can\'t make proper use of Decompression from it. Can some one write a little decomp

相关标签:
1条回答
  • 2021-01-28 23:22

    You asked to read zero bytes and that's what you got. You will need to loop reading chunks of data out of the stream. Keep looping until Read returns zero. Remember that Read returns the number of bytes read.

    I'd use functions like this:

    procedure LZMAcompress(InStream, OutStream: TStream);
    var
      Encoder: TLZMAEncoderStream;
    begin
      Encoder := TLZMAEncoderStream.Create(OutStream, nil);
      try
        Encoder.Write(InStream, InStream.Size);
      finally
        Encoder.Free;
      end;
    end;
    
    procedure LZMAdecompress(InStream, OutStream: TStream; Count: Int64);
    const
      BufferSize = 1024*1024;
    var
      Decoder: TLZMADecoderStream;
      Buffer: TBytes;
      BytesRead, BytesToRead: Integer;
    begin
      Decoder := TLZMADecoderStream.Create(InStream, nil);
      try
        SetLength(Buffer, BufferSize);
        repeat
          BytesToRead := Min(Count, BufferSize);
          BytesRead := Decoder.Read(Buffer, BytesToRead);
          OutStream.Write(Buffer, BytesRead);
          dec(Count, BytesRead);
        until Count=0;
      finally
        Decoder.Free;
      end;
    end;
    

    And there's absolutely no need for memory streams here. Two file streams are what is needed.

    The big issue that you will face is that the library you have chosen to use requires you to know how large the file is that you are decompressing. If you try to read more bytes than are available, then this library code enters a non-terminating loop. Hence my Count parameter in LZMAdecompress.

    My suspicion is that the library that you have chosen to use, or at least the classes that you have chosen to use, are ill suited to your needs. I've only had a quick look through the code but it doesn't look good to me. Any compression library that has a non-terminating loop when presented with invalid data is not very useful. I would shun this library on this evidence. If I were you I would call the LZMA C API directly.

    Perhaps your other problem is that you have made erroneous changes to the library that you are using. Don't do that. Go back to the original version from github.

    0 讨论(0)
提交回复
热议问题