CodeIgniter User Guide Version 2.1.0


Zip 압축 Zip Encoding Class

Zip 압축 클래스를 이용하시면 Zip으로 압축된 파일을 만들 수 있습니다. 압축된것은 데스크탑으로 다운로드 할수도 있고 디렉토리로 저장할수도 있습니다.

클래스 초기화 Initializing the Class

다른 대부분의 클래스들처럼 Zip 클래스도 컨트롤러에서 $this->load->library 함수를 이용하여 초기화 합니다:

$this->load->library('zip');

일단 로드되면 , Zip 라이브러리 객체는$this->zip 로 사용가능합니다.

사용예 Usage Example

이 예제는 어떻게 파일을 압축하고, 서버의 폴더에 저장하고, 데스크탑에 다운로드하는지를 보여줍니다.

$name = 'mydata1.txt';
$data = 'A Data String!';

$this->zip->add_data($name, $data);

// zip 파일을 "my_backup.zip"라는 이름으로 서버의 폴더에 저장합니다.
$this->zip->archive('/path/to/directory/my_backup.zip');

// "my_backup.zip" 파일을 데스크탑에 다운로드 합니다.
$this->zip->download('my_backup.zip');

함수 참조 Function Reference

$this->zip->add_data()

Zip 으로 압축할 데이터를 추가합니다. 첫번째 파라미터는 반드시 파일명을, 두번째 파라미터는 반드시 데이터를 문자열로 포함하고 있어야 합니다:

$name = 'my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);

이 함수를 여러번 호출하여 여러개의 파일을 하나로 압축할수도 있습니다. 예:

$name = 'mydata1.txt';
$data = 'A Data String!';
$this->zip->add_data($name, $data);

$name = 'mydata2.txt';
$data = 'Another Data String!';
$this->zip->add_data($name, $data);

아래와같이 배열로 여러파일을 넘겨줄수도 있습니다:

$data = array(
                'mydata1.txt' => 'A Data String!',
                'mydata2.txt' => 'Another Data String!'
            );

$this->zip->add_data($data);

$this->zip->download('my_backup.zip');

만약 압축된 데이터를 하위폴더로 정리하고싶다면 ,파일명에 경로도 같이 적어주시면 됩니다:

$name = 'personal/my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);

위 예제에서는personal 라는 폴더안에 my_bio.txt 파일이 존재하게 됩니다 .

$this->zip->add_dir()

디렉토리를 추가할때 사용합니다. 일반적으로는 $this->zip->add_data()함수에서 데이터를 하위디렉토리에 놓을수 있기때문에 사용할 필요가 없지만, 빈 폴더를 만들려면 사용해야겠지요 .예제:

$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"

$this->zip->read_file()

서버에 이미존재하는 파일을 압축할때 사용합니다. 파일경로를 넘겨주면 zip 클래스는 그 파일을 읽은후 압축파일에 추가합니다:

$path = '/path/to/photo.jpg';

$this->zip->read_file($path);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

만약 Zip 으로 압축된 파일의 디렉토리 구조를 유지하도록 하고싶다면 , 두번째 파라미터를TRUE 로 설정합니다. 예:

$path = '/path/to/photo.jpg';

$this->zip->read_file($path, TRUE);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

위 예제에서,photo.jpg 파일은 path/to/ 아래 위치하게 됩니다.

$this->zip->read_dir()

서버에 이미 존재하는 폴더 및 폴더내용을 압축할때 사용합니다. 폴더경로를 넘겨주면 zip 클래스는 재귀적으로 읽어들인다음 Zip 으로 압축합니다. 모든 하위폴더 및 파일들이 압축됩니다. 예제 :

$path = '/path/to/your/directory/';

$this->zip->read_dir($path);

// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');

기본적으로 Zip으로 압축된 결과에는 첫번째 파라미터의 모든 경로가 포함됩니다.만약 압축하려는 폴더의 상위 경로명을 포함하지 않으려면 두번째 파라미터를 FALSE 로 두세요 예:

$path = '/path/to/your/directory/';

$this->zip->read_dir($path, FALSE);

이렇게 하면 /path/to/your 경로는 압축파일에서 빠지게됩니다.

$this->zip->archive()

Zip 으로 압축된 파일을 서버의 디렉토리에 생성합니다. 유효한 서버경로와 함께 압축파일의 이름을 넘겨줍니다. 디렉토리는 쓰기 가능해야합니다(666 이나 777 이면 OK). 예:

$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip

$this->zip->download()

서버로부터 Zip 파일을 다운로드 하도록 해줍니다. 이함수에는 생성될 zip 파일명을 넘겨줘야합니다. 예:

$this->zip->download('latest_stuff.zip'); // 파일명은 "latest_stuff.zip"가 될것입니다.

참고 :  이 함수를 호출하는 컨트롤러의 함수에서는 어떤 데이터도 표시(display)하게 해서는 안됩니다. 만약 그럴경우 여러가지 서버 헤더가 전송되게 되므로 다운로드가 실행되게 되며 파일은 바이너리로 취급되게 됩니다.

$this->zip->get_zip()

Zip으로 압축된 파일데이터를 리턴합니다. 데이터에 특별한 작업을 하는경우가 아니라면 이함수를 사용하실일은 잘 없으거예요 . 예:

$name = 'my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);

$zip_file = $this->zip->get_zip();

$this->zip->clear_data()

Zip 클래스는 zip 데이터를 캐시하고 있기때문에 위에서 보여준 각 함수에서 Zip을 다시 컴파일 할 필요가 없습니다. 그러나 만일 , 각각 다른 데이터를 가진 여러개의 Zip 파일을 생성하고자 한다면 , 각각마다 캐시를 제거(clear)할 수 있습니다. 예:

$name = 'my_bio.txt';
$data = 'I was born in an elevator...';

$this->zip->add_data($name, $data);
$zip_file = $this->zip->get_zip();

$this->zip->clear_data();

$name = 'photo.jpg';
$this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents


$this->zip->download('myphotos.zip');