我的问题是,一旦ZipArchive处理完毕,它就会自动关闭并处理掉MemoryStream.如果我在处理ZipArchive信息之前看一下流不完整的拉链.
using (var compressStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(compressStream, ZipArchiveMode.Create))
{
// Adding a couple of entries
string navStackInfo = Navigation.NavState.CurrentStackInfoLines();
var navStackEntry = zipArchive.CreateEntry("NavStack.txt", CompressionLevel.NoCompression);
using (StreamWriter writer = new StreamWriter(navStackEntry.Open()))
{
writer.Write(navStackInfo);
}
var debugInfoEntry = zipArchive.CreateEntry("CallStack.txt", CompressionLevel.Optimal);
using (StreamWriter writer = new StreamWriter(debugInfoEntry.Open()))
{
// debugInfo.Details is a string too
writer.Write(debugInfo.Details);
}
// ...
// compressStream here is not well formed
}
// compressStream here is …Run Code Online (Sandbox Code Playgroud) 我正在使用ZipArchive:
function zip_dir($source, $target){
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
$zip = new \ZipArchive();
if($zip->open($target, \ZipArchive::CREATE) !== true)
exit('cannot create zip');
foreach($iterator as $file){
$zip->addFile($file);
print $file . '<br>';
}
$zip->close();
return $target;
}
zip_dir(__DIR__ . '/test/', __DIR__ . '/testarchive.zip');
Run Code Online (Sandbox Code Playgroud)
我可以看到文件列表,但最后我找不到应该创建的zip文件.我从ZipArchive得到没有错误/异常......
编辑:
之后我添加print $zip->getStatusString();了$zip->close();
并且它打印:无法打开文件:权限被拒绝".这是什么意思?我知道每个目录都是可写的,但是我可以在里面用PHP创建新文件...
编辑2:
if(is_writable(dirname($target)))
print 'target dir is writable...';
Run Code Online (Sandbox Code Playgroud)
它打印出来,所以dir是可写的.感觉就像我在暮光之城......
在我的c#代码中,我正在尝试创建一个zip文件夹供用户在浏览器中下载.所以这里的想法是用户点击下载按钮,他得到一个zip文件夹.
出于测试目的,我使用单个文件并压缩它,但是当它工作时,我将有多个文件.
这是我的代码
var outPutDirectory = AppDomain.CurrentDomain.BaseDirectory;
string logoimage = Path.Combine(outPutDirectory, "images\\error.png"); // I get the file to be zipped
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip");
using (MemoryStream ms = new MemoryStream())
{
// create new ZIP archive within prepared MemoryStream
using (ZipArchive zip = new ZipArchive(ms))
{
zip.CreateEntry(logoimage);
// add some files to ZIP archive
ms.WriteTo(HttpContext.Current.Response.OutputStream);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这件事时,它给了我这个错误
中央目录损坏.
[System.IO.IOException] = {"试图在流的开头之前移动位置."}
异常发生在
使用(ZipArchive zip = new ZipArchive(ms))
有什么想法吗?
PHP 8 改变了 ZIP 存档的“打开”方式,值得注意的是:
不推荐使用空文件作为 ZipArchive。Libzip 1.6.0 不再接受空文件作为有效的 zip 存档。
在下面的测试代码中,打开名为 的 ZIP 文件$backupzip可以正常工作,但打开 ZIP 文件名$invoicezip失败并出现错误:
已弃用:ZipArchive::open():使用空文件,因为 ZipArchive 在第 12 行已弃用
<?php
declare(strict_types=1);
ini_set('display_errors','1');ini_set('display_startup_errors','1');error_reporting(E_ALL);
define('BACKUPDIR','E:\Database_Backups\\');
$backupfile = BACKUPDIR . date('Ymd') . '.zip';
$temp_file = tempnam(sys_get_temp_dir(),'AW');
$backupzip = new ZipArchive();
$invoicezip = new ZipArchive();
$backupzip->open($backupfile,ZipArchive::CREATE); // <<<--- this works
$invoicezip->open($temp_file,ZipArchive::CREATE); // <<<--- this fails
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些PHP代码以便在EC2服务器上的Linux机器上运行,这将从我的S3存储桶读取文件,压缩它们然后将zip文件写回存储桶.
即使从EC2实例的本地磁盘上的某些图像创建一个简单的zip存档,我也立刻遇到了问题,我正在使用脚本从PHP手册中在线测试这个想法,并且还尝试了David的脚本Walsh - http://davidwalsh.name/create-zip-php看起来很棒.既没有产生实际的zip文件,也没有给我不同的状态结果 -
php手册的第一个片段(我添加了变量$ thisdir) -
<?php
$zip = new ZipArchive();
$filename = "test112.zip";
$thisdir = "/uploads/";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>
Run Code Online (Sandbox Code Playgroud)
输出=
numfiles:2状态:11
我在'uploads'文件夹中看不到任何zip文件
我尝试的第二个代码(我不会在这里发布代码) - 我传递真实文件,然后返回
zip存档包含2个状态为0的文件
有什么状态信息.我通过查看phpinfo()的输出来检查是否安装了正确的库; 在ZIP标题下,我看到 - …
我知道如何使用Python提取zip存档,但我究竟如何以百分比显示该提取的进度?
我有一个简单的Zip创建脚本,它将一大堆文件复制到一个目录中,然后从该目录创建一个.zip文件.这种方法听起来很简单,但它产生的档案有问题.
起初我很困惑,因为档案在7Zip,WinRar等等之类的东西中打开很好.但是,我们无法使用Windows内置的存档开启工具.为了排除我的主服务器的任何问题,因为它使用Nginx + PHPfpm + Fedora 16我还使用在Ubuntu服务器上运行的Apache和mod_php在更标准的服务器上进行了测试.
在这两种情况下,问题都是相同的:存档在纯zip中总是打开,但在Windows版本中失败.经过一些随机挖掘后,我想出了在Notepad ++中打开文件以检查其初始标题的想法.
事实证明,Ziparchive()正在做两件不该做的事情.
第一个问题很简单:它将完整路径包含在归档中作为空路径.它不应该,但确实如此.这可能是由于我的递归,所以我可以忍受这一点

第二个问题是导致文件无法打开的大问题.它在归档的最开始处预先设置一个空字节.我所要做的就是在Notepad ++中手动打开文件,删除该字节然后保存它,瞧:文件在包括Windows内置的所有内容中打开都没有问题.
**
我以前从来没有遇到过这种情况,快速谷歌发现Ziparchive()有很多问题,但我找不到像这样的具体内容.
这是我的Zip创建方法:
private function zipcreate($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) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', realpath($file));
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === …Run Code Online (Sandbox Code Playgroud) 我的代码能够打开我已经上传并移动到文件夹的zip文件的问题,zip文件上传很好,你可以在任何Zip程序中打开它然而,当我尝试用ZipArchive打开它来提取数据错误.
$path = "../"; // Upload directory
$count = 0;
foreach ($_FILES['files']['name'] as $f => $name) {
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $name))
$count++; // Number of successfully uploaded file
}
$kioskFile = $_FILES['files']['name'][0];
$kioskFile = explode(".", $kioskFile);
$kioskFile = $kioskFile[0];
$zipFile = "../" . $kioskFile . ".zip";
$zip = new ZipArchive;
$res = $zip->open($zipFile);
if ($res === true) {
$zip->extractTo("./");
$zip->close();
} else {
echo "Error Cannot Open Zip File - Error Code: ";
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,它向我显示错误代码19
ZIPARCHIVE :: ER_NOZIP - 19 …
我对android很新,我正在创建一个有两个微调器的活动,这些微调器从两个单独的API调用接收到的JSONObjects中填充它们的下拉项.出于某种原因,我收到此错误.我不知道为什么我收到此错误或如何解决它.任何人都可以对此有所了解吗?任何帮助将不胜感激!!!这是我的代码:
public class AddPropertyActivity extends AppCompatActivity {
private TextView selectCity;
private static final String TAG = RegisterUserActivity.class.getSimpleName();
private JSONObject jsonObject;
private JSONArray jsonArray;
private ProgressDialog mProgressDialog;
private MyProperty mMyProperty;
private ArrayList<City> cities = new ArrayList<City>();
private ArrayList<City> locations = new ArrayList<City>();
private ArrayList<String> cityNames = new ArrayList<String>();
private ArrayList<String> locationNames = new ArrayList<String>();
private String selectedCity="lahore";
private String selectedLocation="";
private int count=0;
private Spinner citySpinner;
private Spinner locationSpinner;
private DownloadJSON task1;
private DownloadJSON task2;
@Override
protected void onCreate(Bundle savedInstanceState) { …Run Code Online (Sandbox Code Playgroud) 我使用以下脚本将我的目录(在本例中My_Theme)的文件移动到zip存档wordpress.zip.
define('CLIENT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/wp_theme/clients_templates/' . str_replace(' ', '_', $_POST['title']));
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
echo $file . '<br>';
$zip->addFile($file);
}
$zip->close();
Run Code Online (Sandbox Code Playgroud)
现在当我下载并解压缩该文件时,我的文件夹结构如下所示:
我想要的是将目录移动My_Theme到wordpress/wp-content/themes/
结果将是:( wordpress/wp-content/themes/My_Theme包括其中的所有文件和子目录)
我怎样才能做到这一点?