This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_model_file_ops.php
116 lines (87 loc) · 3.16 KB
/
data_model_file_ops.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
// =-=-=-=-=-=-=-
// build the name of the zip file for a given Data Model Directory
function data_model_zip_file_name( $dir, &$file_name ) {
// =-=-=-=-=-=-=-
// peel of the last dir in the path as it
// should be our Data Model Directory
$base = basename( $dir, "/" );
$dir = dirname ( $dir );
// =-=-=-=-=-=-=-
// build a zip file name from the base name
$zip_file = $base . ".zip";
// =-=-=-=-=-=-=-
// set the outvariable with the whole path to the zip
$file_name = $dir . "/" . $zip_file;
return true;
} // data_model_zip_file_name
// =-=-=-=-=-=-=-
// helper fcn to recursively zip a directory
function data_model_recur_zip_dir( $source, $destination, $include_dir = false ) {
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
if (file_exists($destination)) {
unlink ($destination);
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
if ($include_dir) {
$arr = explode("/",$source);
$maindir = $arr[count($arr)- 1];
$source = "";
for ($i=0; $i < count($arr) - 1; $i++) {
$source .= '/' . $arr[$i];
}
$source = substr($source, 1);
$zip->addEmptyDir($maindir);
}
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
} else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
} else if (is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
} // data_model_recur_zip_dir
// =-=-=-=-=-=-=-
// zip a data model directory into a bagit archive
function data_model_zip_dir( $file_name ) {
// =-=-=-=-=-=-=-
// get just the dir name and the name minus
// the zip extension ( data model dir name )
$zip_name = basename( $file_name, ".zip" );
$zip_dir = dirname ( $file_name );
// =-=-=-=-=-=-=-
// get the current directory
$curr_dir = getcwd();
// =-=-=-=-=-=-=-
// change to that dir to add the files
chdir( $zip_dir );
// =-=-=-=-=-=-=-
// recursively zip the whole directory
$ret = data_model_recur_zip_dir( $zip_name, $file_name );
if( false == $ret ) {
error_log( "Failed to write files to zip\n" );
}
// =-=-=-=-=-=-=-
// go back to our orig dir
chdir( $curr_dir );
// =-=-=-=-=-=-=-
// and... were done.
return true;
} // data_model_zip_dir