48 |
let bin2dec num = |
let bin2dec num = |
49 |
let s = string_of_int num in |
let s = string_of_int num in |
50 |
int_of_string ("0b" ^ s) |
int_of_string ("0b" ^ s) |
51 |
|
|
52 |
|
let zip_extract_entry ifile e = |
53 |
|
if e.Zip.is_directory then begin |
54 |
|
try |
55 |
|
Unix.mkdir e.Zip.filename 0o777 |
56 |
|
with Unix.Unix_error(Unix.EEXIST, _, _) -> () |
57 |
|
end else begin |
58 |
|
Zip.copy_entry_to_file ifile e e.Zip.filename |
59 |
|
end |
60 |
|
|
61 |
|
let zip_extract zipfile = |
62 |
|
let ic = Zip.open_in zipfile in |
63 |
|
List.iter (zip_extract_entry ic) (Zip.entries ic); |
64 |
|
Zip.close_in ic |
65 |
|
|
66 |
|
let rec zip_add_entry oc file = |
67 |
|
let s = Unix.stat file in |
68 |
|
match s.Unix.st_kind with |
69 |
|
Unix.S_REG -> |
70 |
|
Zip.copy_file_to_entry file oc ~mtime:s.Unix.st_mtime file |
71 |
|
| Unix.S_DIR -> |
72 |
|
Zip.add_entry "" oc ~mtime:s.Unix.st_mtime |
73 |
|
(if Filename.check_suffix file "/" then file else file ^ "/"); |
74 |
|
let d = Unix.opendir file in |
75 |
|
begin try |
76 |
|
while true do |
77 |
|
let e = Unix.readdir d in |
78 |
|
if e <> "." && e <> ".." then zip_add_entry oc (Filename.concat file e) |
79 |
|
done |
80 |
|
with End_of_file -> () |
81 |
|
end; |
82 |
|
Unix.closedir d |
83 |
|
| _ -> () |
84 |
|
|
85 |
|
let zip_create zipfile files = |
86 |
|
let oc = Zip.open_out zipfile in |
87 |
|
Array.iter (zip_add_entry oc) files; |
88 |
|
Zip.close_out oc |
89 |
|
|
90 |
|
let gz_extract filename = |
91 |
|
begin |
92 |
|
let file = ref "" in |
93 |
|
try |
94 |
|
let buffer = String.create 4096 in |
95 |
|
let file_out = Filename.temp_file "arch_" ".tmp" in |
96 |
|
file := file_out; |
97 |
|
let ic = Gzip.open_in filename in |
98 |
|
let oc = open_out_bin file_out in |
99 |
|
let rec decompress () = |
100 |
|
let n = Gzip.input ic buffer 0 (String.length buffer) in |
101 |
|
if n = 0 then () |
102 |
|
else |
103 |
|
begin |
104 |
|
output oc buffer 0 n; |
105 |
|
decompress() |
106 |
|
end |
107 |
|
in decompress(); |
108 |
|
Gzip.close_in ic; |
109 |
|
close_out oc; |
110 |
|
file_out |
111 |
|
with e -> (try Sys.remove !file with _ -> ()); raise e |
112 |
|
end |
113 |
|
|
114 |
|
open Misc2 |
115 |
|
|
116 |
|
let archive_extract filename archive_type = |
117 |
|
match archive_type with |
118 |
|
"zip" -> zip_extract filename; "" |
119 |
|
| "bz2" -> Misc2.bz2_extract filename |
120 |
|
| "gz" -> gz_extract filename |
121 |
|
| _ -> failwith "wrong archive type %s" archive_type |