Quick.Compression.pas 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. function CompressString(const aStr : string) : string;
  29. function DecompressString(const aStr: string) : string;
  30. implementation
  31. function CompressString(const aStr : string) : string;
  32. var
  33. strstream : TStringStream;
  34. zipstream : TStringStream;
  35. begin
  36. strstream := TStringStream.Create(aStr,TEncoding.UTF8);
  37. try
  38. zipstream := TStringStream.Create('',TEncoding.ANSI);
  39. try
  40. ZCompressStream(strstream, zipstream);
  41. zipstream.Position := 0;
  42. Result := zipstream.DataString;
  43. finally
  44. zipstream.Free;
  45. end;
  46. finally
  47. strstream.Free;
  48. end;
  49. end;
  50. function DecompressString(const aStr: string) : string;
  51. var
  52. strstream : TStringStream;
  53. zipstream : TStringStream;
  54. begin
  55. zipstream := TStringStream.Create(aStr,TEncoding.ANSI);
  56. try
  57. strstream := TStringStream.Create('',TEncoding.UTF8);
  58. try
  59. ZDecompressStream(zipstream, strstream);
  60. strstream.Position := 0;
  61. Result := strstream.DataString;
  62. finally
  63. strstream.Free;
  64. end;
  65. finally
  66. zipstream.Free;
  67. end;
  68. end;
  69. end.