pclzip 라이브러리는 http://www.phpconcept.net 에서 제공하는 무료 소프트웨어로 GNU/LGP를 따릅니다. 먼저, pclzip.lib.php 파일을 다운받아 예제에 작성된 위치에 include 하여 클레스를 호출하면 원하는 작업을 할 수 있습니다.

압축파일 생성, 수정, 삭제시 반환되는 배열은 다음 표와 같습니다.

배 열
filename 원본 파일(또는 폴더)
stored_filename 변경된 파일(또는 폴더)
size 원본 크기
compressed_size 압축된 크기
mtime 수정시간
comment
folder 폴더이면 "1" 반환
index
status 성공하면 "ok"반환
crc


Zip 압축 파일 만들기

파일(또는 폴더)를 압축해 보겠습니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // $create = new PclZip("생성할 압축파일 이름");
 // PclZip 객체에 test.zip 압축파일로 만듭니다.
 $zipfile = new PclZip('test.zip');

 // $create->create("파일 or 디렉토리");
 // $data에 하나의 파일(또는 폴더)이거나 배열형 파일(또는 폴더)가 올 수 있습니다. 
 // test.php 파일과 www 폴더를 현제 경로에 test.zip 파일로 압축하겠습니다.
 $data = array();
 $data = array("test.php", "www");
 $create = $zipfile->create($data);


 echo "<pre>\n";
 print_r($create);
 ?>

www 폴더를 압축하되 test폴더를 만들어 압축해 보겠습니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // $create = new PclZip("생성할 압축파일 이름");
 // PclZip 객체에 test.zip 압축파일로 만듭니다.
 $zipfile = new PclZip('test.zip');

 // $create->create("파일 or 디렉토리", );
 // $data에 하나의 파일(또는 폴더)이거나 배열형 파일(또는 폴더)가 올 수 있습니다. 
 // www 폴더를 현제 경로에 test.zip 파일로 압축하겠습니다.
 // 그리고 $data 뒤에 test 입력하고, 압축하면 stored_filename 변수에
 // 이동경로를 확인할 수 있습니다,
 $data = "www";
 $create = $zipfile->create($data, "test");


 echo "<pre>\n";
 print_r($create);
 ?>


Zip 압축 파일 추가

압축 파일에 새로운 파일을 추가하거나 해당 압축 파일이 없으면 새로 압축 파일을 만듭니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // PclZip 객체를 생성합니다.
 // 추가할 압축파일를 선택합니다. 
 // test.zip 에 파일을 추가하겠습니다.
 $zipfile = new PclZip('test.zip'); 

 // $data에 추가할 파일을 배열로 지정합니다.
 // $data에 하나의 파일(또는 폴더)이거나 배열형 파일(또는 폴더)가 올 수 있습니다. 
 $data = array("index.gif","test.gif");
 $add = $zipfile->add($data);


 echo "<pre>";
 print_r($add);
 ?>


Zip 압축 파일내 Entry 얻기

다음은 압축 파일내 Entry, 즉 파일 목록을 가져 오도록 하겠니다. 반환 값은 파일에 대한 정보를 배열로 반환하기 때문에 적절히 수정해서 사용합니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // PclZip 객체를 생성합니다.
 // 목록을 가져올 파일을 선택합니다.
 $zipfile = new PclZip('test.zip'); 

 // 엔트리 얻기
 $list = $zipfile->listContent();


 echo "<pre>";
 print_r($list);
 /*
 결과:
 Array
 (
    [0] => Array
        (
            [filename] => test/
            [stored_filename] => test/
            [size] => 0
            [compressed_size] => 0
            [mtime] => 1310291714
            [comment] =>
            [folder] => 1
            [index] => 0
            [status] => ok
            [crc] => 0
        )
    [1] => Array
        (
            [filename] => test/lib/
            [stored_filename] => test/lib/
            [size] => 0
            [compressed_size] => 0
            [mtime] => 1310291392
            [comment] =>
            [folder] => 1
            [index] => 1
            [status] => ok
            [crc] => 0
        )
        ...
 }
 */
 ?> 


Zip 압축 파일 해제

압축 파일을 현재 디렉토리에 압축 해제 해보겠습니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // PclZip 객체를 생성합니다.
 //$객체 = new PclZip("해제할 압축파일 이름");
 $zipfile = new PclZip('test.zip'); 

 // 참고로 $extract 변수를 정의하지 않아도 압축해제는 가능합니다.
 $extract = $zipfile->extract(); 


 echo "<pre>";
 print_r($extract);
 ?>

다음은 다른 디렉토리에 압축 파일을 해제하도록 하겠습니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');

 // PclZip 객체를 생성합니다.
 //$객체 = new PclZip("해제할 압축파일 이름");
 $zipfile = new PclZip('test.zip'); 

 // 압축파일을 habony 폴더에 압축해제합니다.
 $extract = $zipfile->extract(PCLZIP_OPT_PATH, './habony/'); 


 echo "<pre>";
 print_r($extract);
 ?>


Zip 압축 파일내 파일 삭제

이제 부분적으로 압축 파일내 파일을 삭제해 보겠습니다.

<?php
 // pcl lib 경로 설정
 include('pclzip.lib.php');


 // PclZip 객체를 생성합니다.
 // 압축파일을 선택합니다.
 $zipfile = new PclZip('test.zip'); 

 // test.gif 를 삭제하겠습니다.
 // 하나의 파일(또는 폴더)이거나 배열형 파일(또는 폴더)가 올 수 있습니다. 
 $delete = $zipfile->delete(PCLZIP_OPT_BY_NAME,"test.gif");


 echo "<pre>";
 print_r($delete);
 ?>

0 댓글