tconend.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. {
  2. This file is part of the Free Pascal packages.
  3. Copyright (c) 1999-2012 by the Free Pascal development team
  4. Tests zip/unzip functionality provided by the FPC zipper.pp unit.
  5. Created by Reinier Olislagers
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. program tconend;
  13. {$mode objfpc}{$h+}
  14. uses SysUtils, classes, zipper, md5;
  15. type
  16. TCallBackHandler = class(TObject)
  17. public
  18. procedure EndOfFile(Sender:TObject; const Ratio:double);
  19. procedure StartOfFile(Sender:TObject; const AFileName:string);
  20. end;
  21. procedure TCallBackHandler.EndOfFile(Sender : TObject; Const Ratio : Double);
  22. begin
  23. writeln('End of file handler hit; ratio: '+floattostr(ratio));
  24. if (Ratio<0) then
  25. begin
  26. writeln('Found compression ratio '+floattostr(Ratio)+', which should never be lower than 0.');
  27. halt(3);
  28. end;
  29. end;
  30. procedure TCallBackHandler.StartOfFile(Sender : TObject; Const AFileName : String);
  31. begin
  32. writeln('Start of file handler hit; filename: '+AFileName);
  33. if AFileName='' then
  34. begin
  35. writeln('Archive filename should not be empty.');
  36. halt(4);
  37. end;
  38. end;
  39. var
  40. code: cardinal;
  41. CallBackHandler: TCallBackHandler;
  42. CompressedFile: string;
  43. FileContents: TStringList;
  44. UncompressedFile1: string;
  45. UncompressedFile1Hash: string;
  46. UncompressedFile2: string;
  47. UncompressedFile2Hash: string;
  48. OurZipper: TZipper;
  49. UnZipper: TUnZipper;
  50. begin
  51. code := 0;
  52. UncompressedFile1:=SysUtils.GetTempFileName('', 'UNC');
  53. UncompressedFile2:=SysUtils.GetTempFileName('', 'UNC');
  54. CompressedFile:=SysUtils.GetTempFileName('', 'ZP');
  55. FileContents:=TStringList.Create;
  56. OurZipper:=TZipper.Create;
  57. UnZipper:=TUnZipper.Create;
  58. CallBackHandler:=TCallBackHandler.Create;
  59. try
  60. // Set up uncompressed files
  61. FileContents.Add('This is an uncompressed file.');
  62. FileContents.Add('And another line.');
  63. FileContents.SaveToFile(UncompressedFile1);
  64. FileContents.Clear;
  65. FileContents.Add('Have you looked into using fpcup today?');
  66. FileContents.Add('It works nicely with fpc and goes well with a fruity red wine, too.');
  67. FileContents.SaveToFile(UncompressedFile2);
  68. // Remember their content, so we can compare later.
  69. UncompressedFile1Hash:=MD5Print(MD5File(UncompressedFile1, MDDefBufSize));
  70. UncompressedFile2Hash:=MD5Print(MD5File(UncompressedFile2, MDDefBufSize));
  71. // Test zip functionality.
  72. writeln('Beginning zip test');
  73. OurZipper.FileName:=CompressedFile;
  74. // Add the files only with their filenames, we don't want to create
  75. // subdirectories:
  76. OurZipper.Entries.AddFileEntry(UncompressedFile1,ExtractFileName(UncompressedFile1));
  77. OurZipper.Entries.AddFileEntry(UncompressedFile2,ExtractFileName(UncompressedFile2));
  78. OurZipper.OnStartFile:[email protected];
  79. OurZipper.OnEndFile:[email protected];
  80. OurZipper.ZipAllFiles;
  81. if not FileExists(CompressedFile) then
  82. begin
  83. writeln('Zip file was not created.');
  84. halt(5);
  85. end;
  86. writeln('Zip test done');
  87. // Delete original files
  88. DeleteFile(UncompressedFile1);
  89. DeleteFile(UncompressedFile2);
  90. // Now unzip
  91. writeln('Beginning unzip test');
  92. Unzipper.FileName:=CompressedFile;
  93. Unzipper.OutputPath:=ExtractFilePath(UncompressedFile1);
  94. UnZipper.OnStartFile:[email protected];
  95. UnZipper.OnEndFile:[email protected];
  96. Unzipper.Examine;
  97. Unzipper.UnZipAllFiles;
  98. // Now we should have the uncompressed files again
  99. if (not FileExists(UncompressedFile1)) or
  100. (not FileExists(UncompressedFile2)) then
  101. begin
  102. writeln('Unzip failed: could not find decompressed files.');
  103. halt(6);
  104. end;
  105. // Compare hashes
  106. if
  107. (UncompressedFile1Hash<>MD5Print(MD5File(UncompressedFile1, MDDefBufSize)))
  108. or
  109. (UncompressedFile2Hash<>MD5Print(MD5File(UncompressedFile2, MDDefBufSize)))
  110. then
  111. begin
  112. writeln('Unzip failed: uncompressed files are not the same as the originals.');
  113. halt(7);
  114. end;
  115. writeln('Unzip test done');
  116. if code = 0 then
  117. writeln('Basic zip/unzip tests passed')
  118. else
  119. writeln('Basic zip/unzip test failed: ', code);
  120. finally
  121. FileContents.Free;
  122. CallBackHandler.Free;
  123. OurZipper.Free;
  124. UnZipper.Free;
  125. try
  126. if FileExists(CompressedFile) then DeleteFile(CompressedFile);
  127. if FileExists(UncompressedFile1) then DeleteFile(UncompressedFile1);
  128. if FileExists(UncompressedFile2) then DeleteFile(UncompressedFile2);
  129. finally
  130. // Ignore errors; operating system should clean out temp files
  131. end;
  132. end;
  133. Halt(code);
  134. end.