在Codeigniter中创建加密的Zip

时间:2013-09-24 15:17:34

标签: php apache codeigniter zip system

在我的Codeigniter控制器中,我有以下代码从数据库中压缩备份文件。问题是,当我解压缩zip文件而不是单个文件时,内部路径中还有其他文件夹。

    $this->load->dbutil();
    $this->load->helper('date');
    $this->load->helper('file');

    set_time_limit(0);

    $prefs = array(
    'format'        => 'txt',                       // gzip, zip, txt
    'filename'      => 'backup.sql',              // File name - NEEDED ONLY WITH ZIP FILES
    'add_drop'      => TRUE,                        // Whether to add DROP TABLE statements to backup file
    'add_insert'    => TRUE,                        // Whether to add INSERT data to backup file
    'newline'       => "\n"                         // Newline character used in backup file
    );


    $backup = $this->dbutil->backup($prefs);


    $file = FCPATH.'GB_dump_backup/backup.txt';
    $zip = FCPATH.'GB_dump_backup/'.now().'backup';

    write_file($file, $backup);

    system('zip -P pass '.$zip.' '.$file .' ');

我不确定为什么zip也会在zip中创建路径文件夹。我的意思是,不是只归档backup.txt,而是从Application文件夹备份到backup.txt文件。解压缩zip文件后,我得到以下图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

zip包含默认情况下创建的zip文件夹中文件的路径。将-j标志添加到j unk路径并仅包含该文件。

system('zip -j -P pass '.$zip.' '.$file .' ');

查看zip的手册页以获取更多信息。

$man zip
...
   -p
   --paths
          Include relative file paths as part of the names of files stored in the ar-
          chive.  This is the default.  The -j option junks the paths and just stores
          the names of the files.