Quick.Compression.pas 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. { ***************************************************************************
  2. Copyright (c) 2016-2017 Kike Pérez
  3. Unit : Quick.Compression
  4. Description : Compression functions
  5. Author : Kike Pérez
  6. Version : 1.1
  7. Created : 14/08/2018
  8. Modified : 20/08/2018
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Compression;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. System.SysUtils,
  27. System.ZLib;
  28. type
  29. TCompressionLevel = TZCompressionLevel;
  30. function CompressString(const aStr : string; aLevel : TCompressionLevel = zcDefault) : string;
  31. function DecompressString(const aStr: string) : string;
  32. implementation
  33. function CompressString(const aStr : string; aLevel : TCompressionLevel = zcDefault) : string;
  34. var
  35. strstream : TStringStream;
  36. zipstream : TStringStream;
  37. begin
  38. strstream := TStringStream.Create(aStr,TEncoding.UTF8);
  39. try
  40. zipstream := TStringStream.Create('',TEncoding.ANSI);
  41. try
  42. ZCompressStream(strstream, zipstream, aLevel);
  43. zipstream.Position := 0;
  44. Result := zipstream.DataString;
  45. finally
  46. zipstream.Free;
  47. end;
  48. finally
  49. strstream.Free;
  50. end;
  51. end;
  52. function DecompressString(const aStr: string) : string;
  53. var
  54. strstream : TStringStream;
  55. zipstream : TStringStream;
  56. begin
  57. zipstream := TStringStream.Create(aStr,TEncoding.ANSI);
  58. try
  59. strstream := TStringStream.Create('',TEncoding.UTF8);
  60. try
  61. ZDecompressStream(zipstream, strstream);
  62. strstream.Position := 0;
  63. Result := strstream.DataString;
  64. finally
  65. strstream.Free;
  66. end;
  67. finally
  68. zipstream.Free;
  69. end;
  70. end;
  71. end.