uses
  SysUtils,
  ddumper,
  dfilereader,
  dbitmem_le,
  dadler32,
  ddeflate,
  dzlib,
  dzlib_decompress,
  dzlib_decompress_utils,
  dzip,
  dzip_utils;

var
  OriginalData: Pointer;

procedure Error(const Msg: PAnsiChar);
begin
  Writeln(stderr, Msg);
  Halt(1);
end;

var
  Buffer: array[0 .. 1024 * 1024 - 1] of Byte;
  BufferEnd: Pointer;
procedure DumpZipInfo(Data, DataEnd: Pointer);
var
  I: LongInt;
  ZipPack: TZipPack;
  ZipFile: TZipFile;
begin
  ZipPack.Init(Data, DataEnd);
  for I := 0 to ZipPack.Count - 1 do begin
    if ZipPack.Get(I, ZipFile) then begin
      Writeln(ZipFile.Name);
      if ZipFile.Unpack(@Buffer[0], @Buffer[0] + Length(Buffer), BufferEnd) then begin
        Writeln('Unpacked size: ', BufferEnd - @Buffer[0]);
        if BufferEnd - @Buffer[0] > 32 then begin
          Dump(@Buffer[0], 32);
        end else
          Dump(@Buffer[0], BufferEnd - @Buffer[0]);
      end else
        Writeln(stderr, 'FAILED UNPACKING');
      ZipFile.Done;
    end;
  end;
  ZipPack.Done;
end;

var
  FileName: AnsiString;
  Data: Pointer;
  Size: SizeUInt;
  F: file of byte;

begin
  FileName := 'zip.zip';
  if not ReadFileContent(FileName, 0, Data, Size) then begin
    Writeln(stderr, 'Cannot read ', FileName);
    Halt(1);
  end;
  OriginalData := Data;
  DumpZipInfo(Data, Data + Size);

  FreeMem(OriginalData);
end.
