minigzip.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. program minigzip;
  2. {
  3. minigzip.c -- simulate gzip using the zlib compression library
  4. Copyright (C) 1995-1998 Jean-loup Gailly.
  5. minigzip is a minimal implementation of the gzip utility. This is
  6. only an example of using zlib and isn't meant to replace the
  7. full-featured gzip. No attempt is made to deal with file systems
  8. limiting names to 14 or 8+3 characters, etc... Error checking is
  9. very limited. So use minigzip only for testing; use gzip for the
  10. real thing. On MSDOS, use only on file names without extension
  11. or in pipe mode.
  12. Pascal tranlastion based on code contributed by Francisco Javier Crespo
  13. Copyright (C) 1998 by Jacques Nomssi Nzali
  14. For conditions of distribution and use, see copyright notice in readme.txt
  15. }
  16. uses
  17. {$IFDEF VER80}
  18. WinCrt,
  19. {$ENDIF}
  20. gzio, zutil;
  21. const
  22. BUFLEN = 16384 ;
  23. GZ_SUFFIX = '.gz' ;
  24. {$DEFINE MAXSEF_64K}
  25. var
  26. buf : packed array [0..BUFLEN-1] of byte; { Global uses BSS instead of stack }
  27. prog : string;
  28. { ERROR =====================================================================
  29. Display error message and exit
  30. ============================================================================}
  31. procedure error (msg:string);
  32. begin
  33. writeln (prog,': ',msg);
  34. halt(1);
  35. end;
  36. { GZ_COMPRESS ===============================================================
  37. Compress input to output then close both files
  38. ============================================================================}
  39. procedure gz_compress (var infile:file; outfile:gzFile);
  40. var
  41. len : uInt;
  42. ioerr : integer;
  43. err : int;
  44. begin
  45. while true do begin
  46. {$I-}
  47. blockread (infile, buf, BUFLEN, len);
  48. {$I+}
  49. ioerr := IOResult;
  50. if (ioerr <> 0) then begin
  51. writeln ('read error: ',ioerr);
  52. halt(1);
  53. end;
  54. if (len = 0) then break;
  55. if (gzwrite (outfile, @buf, len) <> len)
  56. then error (gzerror (outfile, err));
  57. end; {WHILE}
  58. close (infile);
  59. if (gzclose (outfile) <> 0{Z_OK})
  60. then error ('gzclose error');
  61. end;
  62. { GZ_UNCOMPRESS =============================================================
  63. Uncompress input to output then close both files
  64. ============================================================================}
  65. procedure gz_uncompress (infile:gzFile; var outfile:file);
  66. var
  67. len : int;
  68. written : uInt;
  69. ioerr : integer;
  70. err : int;
  71. begin
  72. while true do begin
  73. len := gzread (infile, @buf, BUFLEN);
  74. if (len < 0)
  75. then error (gzerror (infile, err));
  76. if (len = 0)
  77. then break;
  78. {$I-}
  79. blockwrite (outfile, buf, len, written);
  80. {$I+}
  81. if (written <> len)
  82. then error ('write error');
  83. end; {WHILE}
  84. {$I-}
  85. close (outfile);
  86. {$I+}
  87. ioerr := IOResult;
  88. if (ioerr <> 0) then begin
  89. writeln ('close error: ',ioerr);
  90. halt(1);
  91. end;
  92. if (gzclose (infile) <> 0{Z_OK})
  93. then error ('gzclose error');
  94. end;
  95. { FILE_COMPRESS =============================================================
  96. Compress the given file:
  97. create a corresponding .gz file and remove the original
  98. ============================================================================}
  99. procedure file_compress (filename:string; mode:string);
  100. var
  101. infile : file;
  102. outfile : gzFile;
  103. ioerr : integer;
  104. outname : string;
  105. begin
  106. Assign (infile, filename);
  107. {$I-}
  108. Reset (infile,1);
  109. {$I+}
  110. ioerr := IOResult;
  111. if (ioerr <> 0) then begin
  112. writeln ('open error: ',ioerr);
  113. halt(1);
  114. end;
  115. outname := filename + GZ_SUFFIX;
  116. outfile := gzopen (outname, mode);
  117. if (outfile = NIL) then begin
  118. writeln (prog,': can''t gzopen ',outname);
  119. halt(1);
  120. end;
  121. gz_compress(infile, outfile);
  122. erase (infile);
  123. end;
  124. { FILE_UNCOMPRESS ===========================================================
  125. Uncompress the given file and remove the original
  126. ============================================================================}
  127. procedure file_uncompress (filename:string);
  128. var
  129. inname : string;
  130. outname : string;
  131. infile : gzFile;
  132. outfile : file;
  133. ioerr : integer;
  134. len : integer;
  135. begin
  136. len := Length(filename);
  137. if (copy(filename,len-2,3) = GZ_SUFFIX) then begin
  138. inname := filename;
  139. outname := copy(filename,0,len-3);
  140. end
  141. else begin
  142. inname := filename + GZ_SUFFIX;
  143. outname := filename;
  144. end;
  145. infile := gzopen (inname, 'r');
  146. if (infile = NIL) then begin
  147. writeln (prog,': can''t gzopen ',inname);
  148. halt(1);
  149. end;
  150. Assign (outfile, outname);
  151. {$I-}
  152. Rewrite (outfile,1);
  153. {$I+}
  154. ioerr := IOResult;
  155. if (ioerr <> 0) then begin
  156. writeln ('open error: ',ioerr);
  157. halt(1);
  158. end;
  159. gz_uncompress (infile, outfile);
  160. { erase (infile); }
  161. end;
  162. { MINIGZIP =================================================================}
  163. var
  164. uncompr : boolean;
  165. outmode : string[20];
  166. i : integer;
  167. option : string;
  168. begin
  169. uncompr := false;
  170. outmode := 'w6 ';
  171. prog := ParamStr(0);
  172. if (ParamCount = 0) then begin
  173. writeln ('Error: STDIO/STDOUT not supported yet');
  174. writeln;
  175. writeln ('Usage: minigzip [-d] [-f] [-h] [-1 to -9] <file>');
  176. writeln (' -d : decompress');
  177. writeln (' -f : compress with Z_FILTERED');
  178. writeln (' -h : compress with Z_HUFFMAN_ONLY');
  179. writeln (' -1 to -9 : compression level');
  180. exit;
  181. end;
  182. for i:=1 to ParamCount do begin
  183. option := ParamStr(i);
  184. if (option = '-d') then uncompr := true;
  185. if (option = '-f') then outmode[3] := 'f';
  186. if (option = '-h') then outmode[3] := 'h';
  187. if (option[1] = '-') and (option[2] >= '1') and (option[2] <= '9')
  188. then outmode[2] := option[2];
  189. end;
  190. if (uncompr = true)
  191. then file_uncompress (ParamStr(ParamCount))
  192. else file_compress (ParamStr(ParamCount), outmode);
  193. end.