使用流包装器在 zip 存档中创建文件 [英] Create a file in a zip archive using stream wrapper

查看:37
本文介绍了使用流包装器在 zip 存档中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PHP 中使用 zip 流包装器来创建 zip 文件或在 zip 存档中添加文件.这是我的例子:

I want to use a zip stream wrapper in PHP to create zip file or add files in a zip archive. Here are my example :

<?php
echo file_get_contents('zip://file.zip#existing_file.txt');
file_put_contents('zip://file.zip#new_file.txt', 'Trying to put some stuff here...');
echo file_get_contents('zip://file.zip#new_file.txt');

这段代码让我得到这个输出:

This code gets me this output :

$ php test.php 
Hey, I'm an existing text in a file in a zip archive !

Warning: file_put_contents(zip://file.zip#new_file.txt): failed to open stream: operation failed in /home/sylvain/test.php on line 7

Warning: file_get_contents(zip://file.zip#new_file.txt): failed to open stream: operation failed in /home/sylvain/test.php on line 9

当 zip 文件不存在时,我收到同样的错误.

I get the same error when the zip file just doesn't exist.

这里是文件权限:

$ ls -la
total 24
drwxrwxrwx   2 sylvain sylvain  4096 2009-11-13 14:44 .
drwxr-xr-x 134 sylvain sylvain 12288 2009-11-13 14:44 ..
-rwxrwxrwx   1 sylvain sylvain   236 2009-11-13 14:44 file.zip
-rwxrwxrwx   1 sylvain sylvain   268 2009-11-13 14:44 test.php

<小时>

是否可以在不创建新的流包装器的情况下做我想做的事情?


Is it possible to do what I'm trying to do without creating a new stream wrapper ?

推荐答案

zip:// 流包装器不支持写入:http://php.net/manual/en/wrappers.compression.php

zip:// stream wrapper does not support writes: http://php.net/manual/en/wrappers.compression.php

此外,Zip 存档不能写入流中,因为 CRC 位于压缩数据之前

Besides, Zip archives cannot be written in a stream because the CRC comes before the compressed data

这篇关于使用流包装器在 zip 存档中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆