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
<?php
/** -------------------------------------------------------------------------------
 *   ファイルシステム:ZIPファイルの圧縮と解凍のユーザ関数の利用例
 *  2015.09.19 作成 yoshi of CXMedia Inc.
 * -------------------------------------------------------------------------------- */
header("Content-type:text/plain; charset=UTF-8");

// 対象ファイルがあるディレクトリ('data'フォルダ)へ移動
chdir('data/');

// 圧縮対象のファイルパス指定
$in_filepath 'PHP_file_function.txt';

// 解凍先のディレクトリパス指定('data'フォルダ内)
$out_dirpath 'save/';

// 解凍対象のファイルパス指定
$data_ary file2idExtGet($in_filepath);
$zip_filepath $data_ary[0].'.zip';

if ( 
zipfile_add($in_filepath$zip_filepath) ) {
    echo 
"■ZIPファイルの作成:成功\n";
} else {
    echo 
"■ZIPファイルの作成:失敗\n";
}

// Zipアーカイブの展開(解凍)

if ( zipfile_extract($zip_filepath$out_dirpath) ) {
    echo 
"■ZIPファイルの解凍:成功\n";
} else {
    echo 
"■ZIPファイルの解凍:失敗\n";
}

exit;

/* ---------------------------------#
 *     Zip アーカイブの作成
 * -------------------------------- */
function zipfile_add($file,$outpath){
    
$zip = new ZipArchive();
    
//ZIPアーカイブが存在しない場合に、作成
    
if ($zip->open($outpathZipArchive::CREATE)!==TRUE) {
        return 
false;
    }
    
//指定したファイルを追加
    
$zip->addFile($file);
    
$zip->close();
    return 
true;
}
/* ---------------------------------#
 *     Zip アーカイブの展開
 * -------------------------------- */
function zipfile_extract($file,$outpath){
    
$zip = new ZipArchive();
    
//ZIPアーカイブが存在するかのチェック
    
if ($zip->open($file) === TRUE) {
        
// ZIPアーカイブの内容を展開
        
$zip->extractTo$outpath );
        
$zip->close();
        return 
true;
    } else {
        return 
false;
    }
}
/* ------------------------------------------#
 *  ファイル名(パス)と拡張子を分離してget
 * ----------------------------------------- */
function file2idExtGet($str){
    
preg_match("/(.*)\.(.+?)$/",$str,$result);
    if(isset(
$result[2])){return array($result[1],strtolower($result[2]));}
}
?>