ZipArchive - addFile 不起作用

发布于 2024-11-14 11:56:47 字数 876 浏览 9 评论 0原文

情况

  • 我有一个 .zip 文件。在这个.zip中有一个文件夹“dist/”,在文件夹“dist/”中有两件事:1)另一个文件夹“lib/”2)一个jar文件
  • 我打开.zip并提取dist/jar文件。我的磁盘“dist”上将出现一个“新”文件夹,其中包含 jar,
  • 我使用 ZipArchive 打开 jar 文件并在需要时对其进行编辑。编辑由 addFromString() 保存

现在是关键部分

  • ,我想将 jar 文件重新添加到 .zip 中
  • ,我通过执行 $zip->addFile($newdir . 'dist/' . $corefile, ' dist/' . $corefile)

其中:

  • $newdir 是本地磁盘上包含提取/编辑的 jar 文件的“新”dist/ 文件夹的绝对路径
  • $corefile 是该文件的文件名提取/使用的 jar 文件
  • 我想将编辑的 jar 文件添加到 zip 内的 dist/ 文件夹中

会发生什么

  • jar 文件已成功编辑
  • 将 jar 添加回 .zip 时没有给出错误
  • 当我打开修改后的文件时.zip 和 .jar,没有任何改变。

我做错了什么?

谢谢!

--编辑:

发现问题。

将编辑后的 ​​jar 添加回 .zip 后,我在 $zip->close() 之前删除了本地磁盘上的 jar ; close() 显然不起作用,因此,更改没有保存。

我们学到了什么?

在删除/...任何文件之前关闭()!

Situation

  • I have a .zip file. In this .zip is a folder "dist/", in the folder "dist/" are 2 things: 1) another folder "lib/" 2) a jar-file
  • I open the .zip and extract the dist/jar-file. A "new" folder will appear on my disk "dist" with the jar in it
  • I open the jar-file with ZipArchive and edit this where needed. Edits are saved by addFromString()

Now comes the crucial part

  • I want to re-add the jar-file into the .zip
  • I do this by doing $zip->addFile($newdir . 'dist/' . $corefile, 'dist/' . $corefile)

where:

  • $newdir is the absolute path to the "new" dist/ folder with the extracted/edited jar-file in, on my local disk
  • $corefile is the filename of the extracted/used jar-file
  • I want to add the edited jar-file into the dist/ folder inside the zip

What happens

  • The jar-file is successfully edited
  • No errors are given while adding the jar back into the .zip
  • When I open the modified .zip and .jar, nothing is changed.

What am I doing wrong?

Thanks!

--EDIT:

Found the problem.

After I added the edited jar back into the .zip, I emidiatly deleted the jar on my local disk, BEFORE $zip->close() ; the close() obviously didn't work, so, the changes weren't saved.

What did we learn?

close() BEFORE you delete/... any files!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文