minizip.ml 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (***********************************************************************)
  2. (* *)
  3. (* The CamlZip library *)
  4. (* *)
  5. (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
  6. (* *)
  7. (* Copyright 2001 Institut National de Recherche en Informatique et *)
  8. (* en Automatique. All rights reserved. This file is distributed *)
  9. (* under the terms of the GNU Library General Public License, with *)
  10. (* the special exception on linking described in file LICENSE. *)
  11. (* *)
  12. (***********************************************************************)
  13. (* $Id: minizip.ml,v 1.2 2006/04/04 08:29:07 xleroy Exp $ *)
  14. open Printf
  15. let list_entry e =
  16. let t = Unix.localtime e.Zip.mtime in
  17. printf "%6d %6d %c %04d-%02d-%02d %02d:%02d %c %s\n"
  18. e.Zip.uncompressed_size
  19. e.Zip.compressed_size
  20. (match e.Zip.methd with Zip.Stored -> 's' | Zip.Deflated -> 'd')
  21. (t.Unix.tm_year + 1900) (t.Unix.tm_mon + 1) t.Unix.tm_mday
  22. t.Unix.tm_hour t.Unix.tm_min
  23. (if e.Zip.is_directory then 'd' else ' ')
  24. e.Zip.filename;
  25. if e.Zip.comment <> "" then
  26. printf " %s\n" e.Zip.comment
  27. let list zipfile =
  28. let ic = Zip.open_in zipfile in
  29. if Zip.comment ic <> "" then printf "%s\n" (Zip.comment ic);
  30. List.iter list_entry (Zip.entries ic);
  31. Zip.close_in ic
  32. let extract_entry ifile e =
  33. print_string e.Zip.filename; print_newline();
  34. if e.Zip.is_directory then begin
  35. try
  36. Unix.mkdir e.Zip.filename 0o777
  37. with Unix.Unix_error(Unix.EEXIST, _, _) -> ()
  38. end else begin
  39. Zip.copy_entry_to_file ifile e e.Zip.filename
  40. end
  41. let extract zipfile =
  42. let ic = Zip.open_in zipfile in
  43. List.iter (extract_entry ic) (Zip.entries ic);
  44. Zip.close_in ic
  45. let rec add_entry oc file =
  46. let s = Unix.stat file in
  47. match s.Unix.st_kind with
  48. Unix.S_REG ->
  49. printf "Adding file %s\n" file; flush stdout;
  50. Zip.copy_file_to_entry file oc ~mtime:s.Unix.st_mtime file
  51. | Unix.S_DIR ->
  52. printf "Adding directory %s\n" file; flush stdout;
  53. Zip.add_entry "" oc ~mtime:s.Unix.st_mtime
  54. (if Filename.check_suffix file "/" then file else file ^ "/");
  55. let d = Unix.opendir file in
  56. begin try
  57. while true do
  58. let e = Unix.readdir d in
  59. if e <> "." && e <> ".." then add_entry oc (Filename.concat file e)
  60. done
  61. with End_of_file -> ()
  62. end;
  63. Unix.closedir d
  64. | _ -> ()
  65. let create zipfile files =
  66. let oc = Zip.open_out zipfile in
  67. Array.iter (add_entry oc) files;
  68. Zip.close_out oc
  69. let usage() =
  70. prerr_string
  71. "Usage:
  72. minizip t <zipfile> show contents of <zipfile>
  73. minizip x <zipfile> extract files from <zipfile>
  74. minizip c <zipfile> <file> .. create a <zipfile> with the given files";
  75. exit 2
  76. let _ =
  77. if Array.length Sys.argv < 3 then usage();
  78. match Sys.argv.(1) with
  79. "t" -> list Sys.argv.(2)
  80. | "x" -> extract Sys.argv.(2)
  81. | "c" -> create Sys.argv.(2)
  82. (Array.sub Sys.argv 3 (Array.length Sys.argv - 3))
  83. | _ -> usage()