uZipTools.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. unit uZipTools;
  2. ///2013年5月27日 15:26:37
  3. /// 加入对XE支持
  4. ///2013年5月27日 09:35:03
  5. /// 加入对流的压缩函数
  6. interface
  7. uses
  8. ZLib, Windows, Classes, SysUtils;
  9. {$if CompilerVersion>= 21}
  10. {$define NEWZLib}
  11. {$IFEND}
  12. type
  13. //2007以上直接=TBytes
  14. {$if CompilerVersion< 18.5}
  15. TMyBytes = array of Byte;
  16. {$IFEND}
  17. TZipTools = class(TObject)
  18. public
  19. //压缩字符串(与JAVA兼容)
  20. class function compressStr(pvData: string): TBytes;
  21. //解压字符串(与JAVA兼容)
  22. class function unCompressStr(pvData: TBytes; pvDataSize: Integer = 0): string;
  23. //压缩(与JAVA兼容)
  24. class procedure compressStreamEX(const pvStream:TStream);
  25. //解压(与JAVA兼容)
  26. class procedure unCompressStreamEX(const pvStream:TStream);
  27. //压缩(与JAVA兼容)
  28. class function compressStream(const pvStream, pvZipStream:TStream): Boolean;
  29. //解压(与JAVA兼容)
  30. class function unCompressStream(const pvZipStream, pvStream:TStream): Boolean;
  31. //压缩(与JAVA兼容)
  32. class function compressBuf(const Buffer; Count: Longint): TBytes;
  33. //解压(与JAVA兼容)
  34. class function unCompressBuf(const zipBuffer; Count: Longint): TBytes;
  35. class function verifyData(const buf; len:Cardinal):Cardinal;
  36. class function verifyStream(pvStream:TStream; len:Cardinal): Cardinal;
  37. end;
  38. implementation
  39. class function TZipTools.compressBuf(const Buffer; Count: Longint): TBytes;
  40. var
  41. OutBuf: Pointer;
  42. OutBytes: Integer;
  43. begin
  44. {$if defined(NEWZLib)}
  45. ZLib.ZCompress(@Buffer, Count, OutBuf, OutBytes);
  46. {$ELSE}
  47. ZLib.CompressBuf(@Buffer, Count, OutBuf, OutBytes);
  48. {$ifend}
  49. try
  50. SetLength(Result, OutBytes);
  51. CopyMemory(@Result[0], OutBuf, OutBytes);
  52. finally
  53. FreeMem(OutBuf, OutBytes);
  54. end;
  55. end;
  56. class function TZipTools.unCompressBuf(const zipBuffer; Count: Longint): TBytes;
  57. var
  58. lvSize:Cardinal;
  59. OutBuf: Pointer;
  60. OutBytes: Integer;
  61. begin
  62. lvSize := Count;
  63. {$if defined(NEWZLib)}
  64. Zlib.ZDecompress(@zipBuffer, lvSize, OutBuf, OutBytes);
  65. {$ELSE}
  66. Zlib.DecompressBuf(@zipBuffer, lvSize, 0, OutBuf, OutBytes);
  67. {$ifend}
  68. try
  69. SetLength(Result, OutBytes);
  70. CopyMemory(@Result[0], OutBuf, OutBytes);
  71. finally
  72. FreeMem(OutBuf, OutBytes);
  73. end;
  74. end;
  75. class function TZipTools.compressStr(pvData: string): TBytes;
  76. begin
  77. result := compressBuf(PAnsiChar(AnsiString(pvData))^, Length(AnsiString(pvData)));
  78. end;
  79. class procedure TZipTools.compressStreamEX(const pvStream:TStream);
  80. begin
  81. compressStream(pvStream, pvStream);
  82. end;
  83. class function TZipTools.compressStream(const pvStream, pvZipStream:TStream):
  84. Boolean;
  85. var
  86. lvBytes: TBytes;
  87. OutBuf: Pointer;
  88. OutBytes: Integer;
  89. l: Integer;
  90. begin
  91. Result := False;
  92. if pvStream= nil then exit;
  93. l := pvStream.Size;
  94. if l = 0 then Exit;
  95. setLength(lvBytes, l);
  96. pvStream.Position := 0;
  97. pvStream.ReadBuffer(lvBytes[0], l);
  98. {$if defined(NEWZLib)}
  99. ZLib.ZCompress(@lvBytes[0], l, OutBuf, OutBytes);
  100. {$ELSE}
  101. ZLib.CompressBuf(@lvBytes[0], l, OutBuf, OutBytes);
  102. {$ifend}
  103. try
  104. pvZipStream.Size := OutBytes;
  105. pvZipStream.Position := 0;
  106. pvZipStream.WriteBuffer(OutBuf^, OutBytes);
  107. Result := true;
  108. finally
  109. FreeMem(OutBuf, OutBytes);
  110. end;
  111. end;
  112. class procedure TZipTools.unCompressStreamEX(const pvStream:TStream);
  113. begin
  114. unCompressStream(pvStream, pvStream)
  115. end;
  116. class function TZipTools.verifyData(const buf; len: Cardinal): Cardinal;
  117. var
  118. i:Cardinal;
  119. p:PByte;
  120. begin
  121. i := 0;
  122. Result := 0;
  123. p := PByte(@buf);
  124. while i < len do
  125. begin
  126. Result := Result + p^;
  127. Inc(p);
  128. Inc(i);
  129. end;
  130. end;
  131. class function TZipTools.unCompressStream(const pvZipStream, pvStream:TStream):
  132. Boolean;
  133. var
  134. l:Integer;
  135. lvBytes: TBytes;
  136. OutBuf: Pointer;
  137. OutBytes: Integer;
  138. begin
  139. Result := false;
  140. if pvZipStream= nil then exit;
  141. l := pvZipStream.Size;
  142. if l = 0 then Exit;
  143. setLength(lvBytes, l);
  144. pvZipStream.Position := 0;
  145. pvZipStream.ReadBuffer(lvBytes[0], l);
  146. {$if defined(NEWZLib)}
  147. ZLib.ZDecompress(@lvBytes[0], l, OutBuf, OutBytes);
  148. {$ELSE}
  149. Zlib.DecompressBuf(@lvBytes[0], l, 0, OutBuf, OutBytes);
  150. {$ifend}
  151. try
  152. pvStream.Size := OutBytes;
  153. pvStream.Position := 0;
  154. pvStream.WriteBuffer(OutBuf^, OutBytes);
  155. Result := true;
  156. finally
  157. FreeMem(OutBuf, OutBytes);
  158. end;
  159. end;
  160. class function TZipTools.unCompressStr(pvData: TBytes; pvDataSize: Integer =
  161. 0): string;
  162. var
  163. lvSize:Cardinal;
  164. lvOutBytes:TBytes;
  165. s:AnsiString;
  166. begin
  167. lvSize := pvDataSize;
  168. if lvSize = 0 then lvSize := Length(AnsiString(pvData));
  169. lvOutBytes := self.unCompressBuf(pvData[0], lvSize);
  170. SetLength(s, Length(lvOutBytes));
  171. CopyMemory(@s[1], @lvOutBytes[0], Length(lvOutBytes));
  172. Result := s;
  173. end;
  174. class function TZipTools.verifyStream(pvStream:TStream; len:Cardinal): Cardinal;
  175. var
  176. l, j:Cardinal;
  177. lvBytes:TBytes;
  178. begin
  179. SetLength(lvBytes, 1024);
  180. if len = 0 then
  181. begin
  182. j := pvStream.Size - pvStream.Position;
  183. end else
  184. begin
  185. j := len;
  186. end;
  187. Result := 0;
  188. while j > 0 do
  189. begin
  190. if j <1024 then l := j else l := 1024;
  191. pvStream.ReadBuffer(lvBytes[0], l);
  192. Result := verifyData(lvBytes[0], l);
  193. Dec(j, l);
  194. end;
  195. end;
  196. end.