本文整理汇总了PHP中ZipArchive::addFile方法的典型用法代码示例。如果您正苦于以下问题:PHP ZipArchive::addFile方法的具体用法?PHP ZipArchive::addFile怎么用?PHP ZipArchive::addFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZipArchive
的用法示例。
在下文中一共展示了ZipArchive::addFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_zip_package
function create_zip_package($dir, $destination)
{
$zip = new ZipArchive();
if ($zip->open($destination, ZipArchive::OVERWRITE) !== true) {
echo 'Failed to create ' . $destination . ' with code ' . $ret;
return false;
} else {
$zip->addGlob($dir . '/*.php', GLOB_BRACE, array('add_path' => '/' . basename($dir) . '/', 'remove_all_path' => true));
$zip->addFile($dir . '/package-info.xml', 'package-info.xml');
$zip->addFile($dir . '/agreement.txt', 'agreement.' . basename($dir) . '.txt');
$zip->close();
return true;
}
}
示例2: actionIndex
/**
* 程序文件列表
*/
public function actionIndex()
{
if (isset($_POST['id'])) {
$files = $_POST['id'];
if ($files) {
//提交打包
$zip = new ZipArchive();
$name = 'yiifcmsBAK_' . date('YmdHis', time()) . '.zip';
$zipname = WWWPATH . '/' . $name;
//创建一个空的zip文件
if ($zip->open($zipname, ZipArchive::OVERWRITE)) {
foreach ((array) $files as $file) {
if (is_dir($file)) {
//递归检索文件
$allfiles = Helper::scanfDir($file, true);
foreach ((array) $allfiles['files'] as $v) {
$zip->addFile(WWWPATH . '/' . $v, $v);
}
} else {
$zip->addFile(WWWPATH . '/' . $file, $file);
}
}
$zip->close();
//开始下载
Yii::app()->request->sendFile($name, file_get_contents($zipname), '', false);
//下载完成后要进行删除
@unlink($zipname);
} else {
throw new CHttpException('404', 'Failed');
}
}
} else {
$files = Helper::scanfDir(WWWPATH);
asort($files['dirs']);
asort($files['files']);
$files = array_merge($files['dirs'], $files['files']);
$listfiles = array();
foreach ($files as $file) {
$tmpfilename = explode('/', $file);
$filename = end($tmpfilename);
if (is_dir($file)) {
$allfiles = Helper::scanfDir($file, true);
if ($allfiles['files']) {
$filesize = 0;
foreach ((array) $allfiles['files'] as $val) {
$filesize += filesize($val);
}
}
$listfiles[$filename]['type'] = 'dir';
} else {
$filesize = filesize($file);
$listfiles[$filename]['type'] = 'file';
}
$listfiles[$filename]['id'] = $filename;
$listfiles[$filename]['size'] = Helper::byteFormat($filesize);
$listfiles[$filename]['update_time'] = filemtime($filename);
}
}
$this->render('index', array('listfiles' => $listfiles));
}
示例3: writeToFile
public function writeToFile($filename)
{
@unlink($filename);
//if the zip already exists, overwrite it
$zip = new ZipArchive();
if (empty($this->sheets_meta)) {
self::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", no worksheets defined.");
return;
}
if (!$zip->open($filename, ZipArchive::CREATE)) {
self::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", unable to create zip.");
return;
}
$zip->addEmptyDir("docProps/");
$zip->addFromString("docProps/app.xml", self::buildAppXML());
$zip->addFromString("docProps/core.xml", self::buildCoreXML());
$zip->addEmptyDir("_rels/");
$zip->addFromString("_rels/.rels", self::buildRelationshipsXML());
$zip->addEmptyDir("xl/worksheets/");
foreach ($this->sheets_meta as $sheet_meta) {
$zip->addFile($sheet_meta['filename'], "xl/worksheets/" . $sheet_meta['xmlname']);
}
if (!empty($this->shared_strings)) {
$zip->addFile($this->writeSharedStringsXML(), "xl/sharedStrings.xml");
//$zip->addFromString("xl/sharedStrings.xml", self::buildSharedStringsXML() );
}
$zip->addFromString("xl/workbook.xml", self::buildWorkbookXML());
$zip->addFile($this->writeStylesXML(), "xl/styles.xml");
//$zip->addFromString("xl/styles.xml" , self::buildStylesXML() );
$zip->addFromString("[Content_Types].xml", self::buildContentTypesXML());
$zip->addEmptyDir("xl/_rels/");
$zip->addFromString("xl/_rels/workbook.xml.rels", self::buildWorkbookRelsXML());
$zip->close();
}
示例4: pleiofile_add_folder_to_zip
function pleiofile_add_folder_to_zip(ZipArchive &$zip_archive, ElggObject $folder, $folder_path = "")
{
if (!empty($zip_archive) && !empty($folder) && elgg_instanceof($folder, "object", "folder")) {
$folder_title = elgg_get_friendly_title($folder->title);
$zip_archive->addEmptyDir($folder_path . $folder_title);
$folder_path .= $folder_title . DIRECTORY_SEPARATOR;
$file_options = array("type" => "object", "subtype" => "file", "limit" => false, "relationship" => "folder_of", "relationship_guid" => $folder->getGUID());
// add files from this folder to the zip
if ($files = elgg_get_entities_from_relationship($file_options)) {
foreach ($files as $file) {
// check if the file exists
if ($zip_archive->statName($folder_path . $file->originalfilename) === false) {
// doesn't exist, so add
$zip_archive->addFile($file->getFilenameOnFilestore(), $folder_path . sanitize_file_name($file->originalfilename));
} else {
// file name exists, so create a new one
$ext_pos = strrpos($file->originalfilename, ".");
$file_name = substr($file->originalfilename, 0, $ext_pos) . "_" . $file->getGUID() . substr($file->originalfilename, $ext_pos);
$zip_archive->addFile($file->getFilenameOnFilestore(), $folder_path . sanitize_file_name($file_name));
}
}
}
// check if there are subfolders
$folder_options = array("type" => "object", "subtype" => "folder", "limit" => false, "metadata_name_value_pairs" => array("parent_guid" => $folder->getGUID()));
if ($sub_folders = elgg_get_entities_from_metadata($folder_options)) {
foreach ($sub_folders as $sub_folder) {
pleiofile_add_folder_to_zip($zip_archive, $sub_folder, $folder_path);
}
}
}
}
示例5: download
/**
* Recieves an ajax request to download an order
*/
public function download(Request $request)
{
$order = Order::find($request->orderID);
// create new zip opbject
$zip = new \ZipArchive();
// create a temp file & open it
$tmp_file = tempnam('.zip', '');
$file_name = $tmp_file;
$zip->open($tmp_file, \ZipArchive::CREATE);
if (strcmp($order->type, 'Album Order') == 0) {
$selections = $order->albumSelections;
$i = 1;
//loop through all files
foreach ($selections as $selection) {
//add file to the zip
$zip->addFile(public_path($selection->photo->photo_path_high_res), $i . '.jpg');
$i++;
}
} else {
$selections = $order->printsSelections;
$i = 1;
//loop through all files
foreach ($selections as $selection) {
//add file to the zip
$zip->addFile(public_path($selection->photo->photo_path_high_res), $i . ' ' . $selection->format->format . ' ' . $selection->quantity . '.jpg');
$i++;
}
}
// close zip
$zip->close();
//send the file to the browser as a download
return response()->download($tmp_file, 'orders.zip')->deleteFileAfterSend(true);
}
示例6: zip
public static function zip($source, $destination, $exclude = '')
{
if (extension_loaded('zip') === true) {
if (file_exists($source) === true) {
$zip = new ZipArchive();
if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) {
$source = realpath($source);
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if (strpos($file, $exclude) == 0) {
$file = realpath($file);
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . DIRECTORY_SEPARATOR, '', $file . DIRECTORY_SEPARATOR));
} else {
if (is_file($file) === true) {
$zip->addFile($file, str_replace($source . DIRECTORY_SEPARATOR, '', $file));
}
}
}
}
} else {
if (is_file($source) === true) {
$zip->addFile($source, basename($source));
//$zip->addFromString(basename($source), file_get_contents($source));
}
}
}
return $zip->close();
}
}
return false;
}
示例7: zipFiles
/**
* Zip files
*
* @param string $targetDir Target dir path
* @param array $files Files to zip
*
* @throws \Exception
*/
private function zipFiles($targetDir, $files)
{
$zip = new \ZipArchive();
$zipName = pathinfo($files[0], PATHINFO_FILENAME);
$zipPath = FileSystem::getUniquePath($targetDir . DIRECTORY_SEPARATOR . $zipName . ".zip");
if ($zip->open($zipPath, \ZipArchive::CREATE)) {
foreach ($files as $file) {
$path = $targetDir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
$zip->addEmptyDir($file);
foreach (Finder::find("*")->from($path) as $item) {
$name = $file . DIRECTORY_SEPARATOR . substr_replace($item->getPathname(), "", 0, strlen($path) + 1);
if ($item->isDir()) {
$zip->addEmptyDir($name);
} else {
$zip->addFile($item->getRealPath(), $name);
}
}
} else {
$zip->addFile($path, $file);
}
}
$zip->close();
} else {
throw new \Exception("Can not create ZIP archive '{$zipPath}' from '{$targetDir}'.");
}
}
示例8: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$info = $this->container->info()->get();
$path = $this->container->path();
$vers = $info['version'];
$filter = '/' . implode('|', $this->excludes) . '/i';
$this->line(sprintf('Starting: webpack'));
exec('webpack -p');
$finder = Finder::create()->files()->in($path)->ignoreVCS(true)->filter(function ($file) use($filter) {
return !preg_match($filter, $file->getRelativePathname());
});
$zip = new \ZipArchive();
if (!$zip->open($zipFile = "{$path}/pagekit-" . $vers . ".zip", \ZipArchive::OVERWRITE)) {
$this->abort("Can't open ZIP extension in '{$zipFile}'");
}
foreach ($finder as $file) {
$zip->addFile($file->getPathname(), $file->getRelativePathname());
}
$zip->addFile("{$path}/.bowerrc", '.bowerrc');
$zip->addFile("{$path}/.htaccess", '.htaccess');
$zip->addEmptyDir('tmp/');
$zip->addEmptyDir('tmp/cache');
$zip->addEmptyDir('tmp/temp');
$zip->addEmptyDir('tmp/logs');
$zip->addEmptyDir('tmp/sessions');
$zip->addEmptyDir('tmp/packages');
$zip->addEmptyDir('app/database');
$zip->close();
$name = basename($zipFile);
$size = filesize($zipFile) / 1024 / 1024;
$this->line(sprintf('Building: %s (%.2f MB)', $name, $size));
}
示例9: zip
/**
* Creates a zip file from a file or a folder recursively without a full nested folder structure inside the zip file
* Based on: http://stackoverflow.com/a/1334949/3073849
* @param string $source The path of the folder you want to zip
* @param string $destination The path of the zip file you want to create
* @return bool Returns TRUE on success or FALSE on failure.
*/
public static function zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source)) {
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $path) {
$path = str_replace('\\', '/', $path);
$path = realpath($path);
if (is_dir($path)) {
$zip->addEmptyDir(str_replace($source . '/', '', $path . '/'));
} elseif (is_file($path)) {
$zip->addFile($path, str_replace($source . '/', '', $path));
}
}
} elseif (is_file($source)) {
$zip->addFile($source, basename($source));
}
return $zip->close();
}
示例10: actionZip
/**
* Exports an add-on's XML data.
*
* @return XenForo_ControllerResponse_Abstract
*/
public function actionZip()
{
$addOnId = $this->_input->filterSingle('addon_id', XenForo_Input::STRING);
$addOn = $this->_getAddOnOrError($addOnId);
$rootDir = XenForo_Application::getInstance()->getRootDir();
$zipPath = XenForo_Helper_File::getTempDir() . '/addon-' . $addOnId . '.zip';
if (file_exists($zipPath)) {
unlink($zipPath);
}
$zip = new ZipArchive();
$res = $zip->open($zipPath, ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFromString('addon-' . $addOnId . '.xml', $this->_getAddOnModel()->getAddOnXml($addOn)->saveXml());
if (is_dir($rootDir . '/library/' . $addOnId)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootDir . '/library/' . $addOnId));
foreach ($iterator as $key => $value) {
$zip->addFile(realpath($key), str_replace($rootDir . '/', '', $key));
}
}
if (is_dir($rootDir . '/js/' . strtolower($addOnId))) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootDir . '/js/' . strtolower($addOnId)));
foreach ($iterator as $key => $value) {
$zip->addFile(realpath($key), str_replace($rootDir . '/', '', $key));
}
}
$zip->close();
}
if (!file_exists($zipPath) || !is_readable($zipPath)) {
return $this->responseError(new XenForo_Phrase('devkit_error_while_creating_zip'));
}
$this->_routeMatch->setResponseType('raw');
$attachment = array('filename' => 'addon-' . $addOnId . '_' . $addOn['version_string'] . '.zip', 'file_size' => filesize($zipPath), 'attach_date' => XenForo_Application::$time);
$viewParams = array('attachment' => $attachment, 'attachmentFile' => $zipPath);
return $this->responseView('XenForo_ViewAdmin_Attachment_View', '', $viewParams);
}
示例11: export
public static function export(\Rebond\Core\ModelInterface $module)
{
$exportPath = \Rebond\Config::path('export');
$tempPath = \Rebond\Config::path('temp');
// TODO add XML model node
// generate data script
$dataScriptPath = $tempPath . 'data.sql';
$table = 'app_' . strtolower($module->getTitle());
$db = new Util\Data();
$result = $db->select('SELECT * FROM ' . $table);
$script = $db->backupData($table, $result);
$result = $db->select('SELECT * FROM cms_content WHERE module_id = ?', [$module->getId()]);
$script .= $db->backupData('cms_content', $result);
File::save($dataScriptPath, 'w', $script);
// create zip
$zipFile = $module->getTitle() . '.zip';
$zip = new \ZipArchive();
$res = $zip->open($exportPath . $zipFile, \ZIPARCHIVE::OVERWRITE);
if (!$res) {
return $res;
}
$modulePath = FULL_PATH . 'Rebond/App/' . $module->getTitle() . '/';
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($modulePath));
foreach ($iterator as $file) {
$filename = str_replace($modulePath, '', str_replace('\\', '/', $file));
if (file_exists($file) && substr($filename, -1) != '.') {
$zip->addFile($file, $filename);
}
}
$zip->addFile($dataScriptPath, 'data.sql');
$zip->close();
return $zipFile;
}
示例12: download_zip
function download_zip()
{
$filename = "tmp/{$locale}.zip";
if (is_file($filename)) {
unlink($filename);
}
$zip = new ZipArchive();
$zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
$zip->addFile(LANG_DIR . "/{$locale}.php", "{$locale}.php");
$zip->addEmptyDir($locale);
$dir = opendir(LANG_DIR . "/" . $locale);
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != ".." && $file != "CVS") {
$zip->addFile(LANG_DIR . "/{$locale}/{$file}", "{$locale}/{$file}");
}
}
closedir($dir);
$zip->close();
header("Cache-Control: public");
header("Expires: -1");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-Type: application/zip");
header("Content-Length: " . (string) filesize($filename));
header("Content-Disposition: 'attachment'; filename=\"{$locale}.zip\"");
header("Content-Transfer-Encoding: binary");
readfile($filename);
die;
}
示例13: create_zip
public function create_zip($facility_code)
{
$this->update_rollout_status($facility_code);
$this->create_core_tables($facility_code);
$this->create_bat($facility_code);
$sql_filepath = 'tmp/' . $facility_code . '.sql';
$expected_zip_sql_filepath = $facility_code . '/' . $facility_code . '.sql';
$bat_filepath = 'tmp/' . $facility_code . '_install_db.bat';
$expected_zip_bat_filepath = $facility_code . '/' . $facility_code . '_install_db.bat';
$ini_filepath = 'offline/my.ini';
$expected_ini_filepath = $facility_code . '/' . 'my.ini';
$expected_old_filepath = $facility_code . '/old/';
$zip = new ZipArchive();
$zip_name = $facility_code . '.zip';
$zip->open($zip_name, ZipArchive::CREATE);
$zip->addFile($sql_filepath, ltrim($expected_zip_sql_filepath, '/'));
$zip->addFile($bat_filepath, ltrim($expected_zip_bat_filepath, '/'));
$zip->addEmptyDir($expected_old_filepath);
$zip->addFile($ini_filepath, ltrim($expected_ini_filepath, '/'));
$zip->close();
// ob_end_clean();
header("Cache-Control: public");
header("Content-Description: File Transfer");
// header("Content-Length: ". filesize("$zip_name").";");
header("Content-Disposition: attachment; filename={$zip_name}");
header("Content-type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($zip_name);
unlink($sql_filepath);
unlink($bat_filepath);
unlink($zip_name);
// echo "$final_output_bat";
// echo "I worked";
// echo "Expecto patronum";
}
示例14: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$file = 'build/portable-zip-api.zip';
$pharCommand = $this->getApplication()->find('build:phar');
$arrayInput = new ArrayInput(array('command' => 'build:phar'));
$arrayInput->setInteractive(false);
if ($pharCommand->run($arrayInput, $output)) {
$output->writeln('The operation is aborted due to build:phar command');
return 1;
}
if (file_exists($file)) {
$output->writeln('Removing previous package');
unlink($file);
}
$zip = new \ZipArchive();
if ($zip->open($file, \ZipArchive::CREATE) !== true) {
$output->writeln('Failed to open zip archive');
return 1;
}
$zip->addFile('build/zip.phar.php', 'zip.phar.php');
$zip->addFile('app/zip.sqlite.db', 'zip.sqlite.db');
$zip->addFile('README.md', 'README.md');
$zip->close();
return 0;
}
示例15: actionIndex
/**
* 程序文件列表
*/
public function actionIndex()
{
if (Yii::app()->request->isPostRequest) {
$files = Yii::app()->request->getParam('id');
if ($files) {
//提交打包
$zip = new ZipArchive();
$name = 'yiifcmsBAK_' . date('YmdHis', time()) . '.zip';
$zipname = ROOT_PATH . '/' . $name;
//创建一个空的zip文件
if ($zip->open($zipname, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
foreach ($files as $file) {
if (is_dir($file)) {
//递归检索文件
$allfiles = Helper::scanfDir($file, true);
foreach ($allfiles['files'] as $v) {
$zip->addFile(ROOT_PATH . '/' . $v, $v);
}
} else {
$zip->addFile(ROOT_PATH . '/' . $file, $file);
}
}
} else {
$this->message('error', 'An error occurred creating your ZIP file.');
}
$zip->close();
Yii::app()->request->sendFile($name, file_get_contents($zipname), '', false);
//下载完成后要进行删除
@unlink($zipname);
}
} else {
$all_files = Helper::scanfDir(ROOT_PATH);
asort($all_files['dirs']);
asort($all_files['files']);
$files = array_merge($all_files['dirs'], $all_files['files']);
$listfiles = array();
foreach ($files as $file) {
$tmpfilename = explode('/', $file);
$filename = end($tmpfilename);
if (is_dir($file)) {
$allfiles = Helper::scanfDir($file, true);
if ($allfiles['files']) {
$filesize = 0;
foreach ($allfiles['files'] as $val) {
$filesize += filesize($val);
}
}
$listfiles[$filename]['type'] = 'dir';
} else {
$filesize = filesize($file);
$listfiles[$filename]['type'] = 'file';
}
$listfiles[$filename]['id'] = $filename;
$listfiles[$filename]['size'] = Helper::byteFormat($filesize);
$listfiles[$filename]['update_time'] = filemtime($filename);
}
$this->render('index', array('listfiles' => $listfiles));
}
}