php
PHP - PCLTAR LIBRARY 를 이용한 TAR 압축, 해제
pcltar 라이브러리는 http://www.phpconcept.net 에서 제공하는 무료 소프트웨어로 GNU/LGP를 따릅니다. 먼저, pcltar.lib.zip 파일을 다운받아 원하는 위치에 압축을 풀어줍니다.
Tar 압축파일 만들기
다운 받은 파일을 정상적으로 설치하였다면, tar.gzip 으로 파일(또는 폴더)를 압축해 보겠습니다.<?php
// library 가 위치하는 경로를 작성합니다.
// 기본 지정 폴더는 lib 입니다
if(!defined("PCLTAR_LIB_DIR")){
define("PCLTAR_LIB_DIR", "lib");
}
$g_pcltar_lib_dir = PCLTAR_LIB_DIR;
include (PCLTAR_LIB_DIR . "/pclerror.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltrace.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltar.lib.php3");
// PclTarCreate($tarname, $list);
// $list 에 하나의 파일(또는 폴더)이거나
// 배열형인 파일(또는 폴더)가 올 수 있습니다.
// $tarname 에 압축할 파일명을 지정합니다.
// habony폴더를 test.tgz로 압축해 보겠습니다.
if(($err = PclTarCreate("test.tgz","habony")) === 1){
echo "압축 파일 작성 성공!";
} else {
echo "압축파일 실패! 오류 코드:" . $err;
}
?>
Tar 압축 파일 추가
압축 파일에 새로운 파일을 추가하거나 해당 압축 파일이 없으면 새로 압축 파일을 만듭니다.<?php
// library 가 위치하는 경로를 작성합니다.
// 기본 폴더는 lib 입니다
if(!defined("PCLTAR_LIB_DIR")){
define("PCLTAR_LIB_DIR", "lib");
}
$g_pcltar_lib_dir = PCLTAR_LIB_DIR;
include (PCLTAR_LIB_DIR . "/pclerror.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltrace.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltar.lib.php3");
// PclTarAdd($tarname, $list);
// $tarname 에 압축파일를 선택하여
// 추가할 파일(또는 폴더)를 지정합니다.
// $list 에 하나의 파일(또는 폴더)이거나
// 배열형인 파일(또는 폴더)가 올 수 있습니다.
$list = array("index.gif", "chmod.php");
if(($err = PclTarAdd("test.tgz", $list)) === 1){
echo "압축 파일 추가 성공!";
} else {
echo "압축파일 추가 실패! 오류 코드:" . $err;
}
?>
Tar 압축 파일 해제
Tar 압축 해제입니다.<?php
// library 가 위치하는 경로를 작성합니다.
// 기본 폴더는 lib 입니다
if(!defined("PCLTAR_LIB_DIR")){
define("PCLTAR_LIB_DIR", "lib");
}
$g_pcltar_lib_dir = PCLTAR_LIB_DIR;
include (PCLTAR_LIB_DIR . "/pclerror.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltrace.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltar.lib.php3");
// PclTarExtract($tarname, $dir);
// $tarname 에 해제할 압축파일를 선택합니다.
// $dir 은 해제할 경로를 지정합니다.
// 다음은 test.tgz 를 test폴더에 해제합니다.
$list = PclTarExtract("test.tgz", "test");
echo "<pre>";
print_r($list);
/*
결과:
Array
(
[0] => Array
(
[checksum] => 2843
[filename] => ./test/tar/retto
[mode] => 16895
[uid] => 0
[gid] => 0
[size] => 0
[mtime] => 1310291714
[typeflag] => 5
[status] => already_a_directory
)
[1] => Array
(
[checksum] => 3411
[filename] => ./test/tar/retto/admin
[mode] => 16895
[uid] => 0
[gid] => 0
[size] => 0
[mtime] => 1310291392
[typeflag] => 5
[status] => already_a_directory
)
...
}
*/
?>
Tar 압축 파일내 파일 삭제
이제 부분적으로 압축 파일내 파일을 삭제해 보겠습니다.<?php
// library 가 위치하는 경로를 작성합니다.
// 기본 폴더는 lib 입니다
if(!defined("PCLTAR_LIB_DIR")){
define("PCLTAR_LIB_DIR", "lib");
}
$g_pcltar_lib_dir = PCLTAR_LIB_DIR;
include (PCLTAR_LIB_DIR . "/pclerror.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltrace.lib.php3");
include (PCLTAR_LIB_DIR . "/pcltar.lib.php3");
// PclTarDelete($tarname, $list);
// $tarname 에 부분 삭제할 압축파일를 선택합니다.
// $list 에 하나의 파일(또는 폴더)이거나
// 배열형인 파일(또는 폴더)가 올 수 있습니다.
$list = array("index.gif","chmod.php");
$delete = PclTarDelete("test.tgz", $list);
echo "<pre>";
print_r($delete);
/*
결과:
Array
(
[0] => Array
(
[checksum] => 2843
[filename] => ./test/tar/retto
[mode] => 16895
[uid] => 0
[gid] => 0
[size] => 0
[mtime] => 1310291714
[typeflag] => 5
[status] => already_a_directory
)
[1] => Array
(
[checksum] => 3411
[filename] => ./test/tar/retto/admin
[mode] => 16895
[uid] => 0
[gid] => 0
[size] => 0
[mtime] => 1310291392
[typeflag] => 5
[status] => already_a_directory
)
...
}
*/
?>
0 댓글