<commit_before>(*
 *	 Unit owner: D10.Mofen
 *         homePage: http://www.diocp.org
 *	       blog: http://www.cnblogs.com/dksoft
 *
 *   2015-02-22 08:29:43
 *     DIOCP-V5 
 *
 *
 *)
unit utils.zipTools;

interface

{$if CompilerVersion>= 21}
  {$define NEWZLib}
{$IFEND}

uses
  Classes, Zlib, SysUtils;

type
{$if CompilerVersion< 18.5}
  TBytes = array of Byte;
{$IFEND}

  TZipTools = class(TObject)
  public
    /// <summary>
    ///   ѹ
    /// </summary>
    class procedure UnZipStream(const pvInStream, pvOutStream: TStream);

    /// <summary>
    ///   ѹ
    /// </summary>
    class procedure ZipStream(const pvInStream, pvOutStream: TStream);


    class function verifyData(const buf; len:Cardinal): Cardinal;
    class function verifyStream(pvStream:TStream; len:Cardinal): Cardinal;
  end;

implementation

class procedure TZipTools.UnZipStream(const pvInStream, pvOutStream: TStream);
{$if defined(NEWZLib)}
var
  lvBytes, lvOutBytes:TBytes;
{$ELSE}
var
  l:Integer;
  lvBytes: TBytes;
  OutBuf: Pointer;
  OutBytes: Integer;
{$ifend}
begin
  {$if defined(NEWZLib)}
  SetLength(lvBytes, pvInStream.Size);
  pvInStream.Position := 0;
  pvInStream.Read(lvBytes[0], pvInStream.Size);
  ZLib.ZDecompress(lvBytes, lvOutBytes);
  pvOutStream.Size := 0;
  pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes));
  {$ELSE}
  if pvInStream= nil then exit;
  l := pvInStream.Size;
  if l = 0 then Exit;

  setLength(lvBytes, l);
  pvInStream.Position := 0;
  pvInStream.ReadBuffer(lvBytes[0], l);
  Zlib.DecompressBuf(@lvBytes[0], l, 0, OutBuf, OutBytes);
  try
    pvOutStream.Size := OutBytes;
    pvOutStream.Position := 0;
    pvOutStream.WriteBuffer(OutBuf^, OutBytes);
  finally
    FreeMem(OutBuf, OutBytes);
  end;
  {$ifend}



//  if pvInStream= nil then Exit;
//  if pvInStream.Size = 0 then Exit;
//  ZLib.ZDecompressStream(pvInStream, pvOutStream);
end;

class function TZipTools.verifyData(const buf; len: Cardinal): Cardinal;
var
  i:Cardinal;
  p:PByte;
begin
  i := 0;
  Result := 0;
  p := PByte(@buf);
  while i < len do
  begin
    Result := Result + p^;
    Inc(p);
    Inc(i);
  end;
end;

class function TZipTools.verifyStream(pvStream:TStream; len:Cardinal):
    Cardinal;
var
  l, j:Cardinal;
  lvBytes:TBytes;
begin
  SetLength(lvBytes, 1024);

  if len = 0 then
  begin
    j := pvStream.Size - pvStream.Position;
  end else
  begin
    j := len;
  end;

  Result := 0;

  while j > 0 do
  begin
    if j <1024 then l := j else l := 1024;

    pvStream.ReadBuffer(lvBytes[0], l);

    Result := Result + verifyData(lvBytes[0], l);
    Dec(j, l);
  end;
end;

class procedure TZipTools.ZipStream(const pvInStream, pvOutStream: TStream);
{$if defined(NEWZLib)}
var
  lvBytes, lvOutBytes:TBytes;
{$ELSE}
var
  lvBytes: TBytes;
  OutBuf: Pointer;
  OutBytes: Integer;
  l: Integer;
{$ifend}
begin
{$if defined(NEWZLib)}
  SetLength(lvBytes, pvInStream.Size);
  pvInStream.Position := 0;
  pvInStream.Read(lvBytes[0], pvInStream.Size);
  ZLib.ZCompress(lvBytes, lvOutBytes);

  pvOutStream.Size := 0;
  pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes));
{$ELSE}
  if pvInStream= nil then exit;

  l := pvInStream.Size;

  if l = 0 then Exit;

  setLength(lvBytes, l);
  pvInStream.Position := 0;
  pvInStream.ReadBuffer(lvBytes[0], l);

  ZLib.CompressBuf(@lvBytes[0], l, OutBuf, OutBytes);
  try
    pvOutStream.Size := OutBytes;
    pvOutStream.Position := 0;
    pvOutStream.WriteBuffer(OutBuf^, OutBytes);
  finally
    FreeMem(OutBuf, OutBytes);
  end;
{$ifend}


  //ZLib.ZDecompress(pvInStream, pvOutStream);
end;

end.<commit_msg>* 修复utils.ziptools.pas在2010下面编译出错的bug<commit_after>(*
 *	 Unit owner: D10.Mofen
 *         homePage: http://www.diocp.org
 *	       blog: http://www.cnblogs.com/dksoft
 *
 *   2015-02-22 08:29:43
 *     DIOCP-V5 
 *
 *
 *)
unit utils.zipTools;

interface

{$if CompilerVersion>= 21}
  {$define NEWZLib}
{$IFEND}

uses
  Classes, Zlib, SysUtils;

type
{$if CompilerVersion< 18.5}
  TBytes = array of Byte;
{$IFEND}

  TZipTools = class(TObject)
  public
    /// <summary>
    ///   ѹ
    /// </summary>
    class procedure UnZipStream(const pvInStream, pvOutStream: TStream);

    /// <summary>
    ///   ѹ
    /// </summary>
    class procedure ZipStream(const pvInStream, pvOutStream: TStream);


    class function verifyData(const buf; len:Cardinal): Cardinal;
    class function verifyStream(pvStream:TStream; len:Cardinal): Cardinal;
  end;

implementation

class procedure TZipTools.UnZipStream(const pvInStream, pvOutStream: TStream);
var
  lvBytes:TBytes;
  l:Integer;
  OutBuf: Pointer;
  OutBytes: Integer;
begin
  if pvInStream= nil then exit;
  l := pvInStream.Size;
  if l = 0 then Exit;
  setLength(lvBytes, l);
  pvInStream.Position := 0;
  pvInStream.ReadBuffer(lvBytes[0], l);
  {$if defined(NEWZLib)}
  ZLib.ZDecompress(@lvBytes[0], l, OutBuf, OutBytes);
  {$ELSE}
  Zlib.DecompressBuf(@lvBytes[0], l, 0, OutBuf, OutBytes);
  {$ifend}
  try
    pvOutStream.Size := OutBytes;
    pvOutStream.Position := 0;
    pvOutStream.WriteBuffer(OutBuf^, OutBytes);
  finally
    FreeMem(OutBuf, OutBytes);
  end;
end;

class function TZipTools.verifyData(const buf; len: Cardinal): Cardinal;
var
  i:Cardinal;
  p:PByte;
begin
  i := 0;
  Result := 0;
  p := PByte(@buf);
  while i < len do
  begin
    Result := Result + p^;
    Inc(p);
    Inc(i);
  end;
end;

class function TZipTools.verifyStream(pvStream:TStream; len:Cardinal):
    Cardinal;
var
  l, j:Cardinal;
  lvBytes:TBytes;
begin
  SetLength(lvBytes, 1024);

  if len = 0 then
  begin
    j := pvStream.Size - pvStream.Position;
  end else
  begin
    j := len;
  end;

  Result := 0;

  while j > 0 do
  begin
    if j <1024 then l := j else l := 1024;

    pvStream.ReadBuffer(lvBytes[0], l);

    Result := Result + verifyData(lvBytes[0], l);
    Dec(j, l);
  end;
end;

class procedure TZipTools.ZipStream(const pvInStream, pvOutStream: TStream);
var
  lvInBuf: TBytes;
  OutBuf: Pointer;
  OutBytes: Integer;
  l: Integer;

begin
  if pvInStream= nil then exit;
  l := pvInStream.Size;
  if l = 0 then Exit;

  SetLength(lvInBuf, l);
  pvInStream.Position := 0;
  pvInStream.ReadBuffer(lvInBuf[0], l);
{$if defined(NEWZLib)}
  ZLib.ZCompress(@lvInBuf[0], l, OutBuf, OutBytes);
{$ELSE}
  ZLib.CompressBuf(@lvInBuf[0], l, OutBuf, OutBytes);
{$ifend}
  try
    pvOutStream.Size := OutBytes;
    pvOutStream.Position := 0;
    pvOutStream.WriteBuffer(OutBuf^, OutBytes);
  finally
    FreeMem(OutBuf, OutBytes);
  end;

end;

end.
{'fraction_non_alphanumeric': 0.1368762552139657, 'fraction_numerical': 0.018384056851537153, 'mean_word_length': 3.5431578947368423, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5417208', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2461, 'n_words': 573}