使用PHP列出.7z,.rar和.tar档案中的文件(List files in .7z, .rar and .tar archives using PHP)
我想列出档案内的文件 , 而不用提取 。
我感兴趣的档案类型:
- .7z(7-Zip)
- .rar(WinRAR)
- .tar(POSIX,例如GNU tar)。
- .zip(ISO标准,例如WinZip)
对于.zip文件 ,我已经能够实现这一点:
<?php $za = new ZipArchive(); $za->open('theZip.zip'); for ($i = 0; $i < $za->numFiles; $i++) { $stat = $za->statIndex($i); print_r(basename($stat['name']) . PHP_EOL); } ?>
但是,我还没有设法为.7z文件做同样的事情。 没有测试.rar和.tar,但也需要它们。
I want to list the files inside an archive, without their extraction.
The types of archives I am interested in:
- .7z (7-Zip)
- .rar (WinRAR)
- .tar (POSIX, e.g. GNU tar).
- .zip (ISO standard, e.g. WinZip)
For .zip files, I have been able to achieve this:
<?php $za = new ZipArchive(); $za->open('theZip.zip'); for ($i = 0; $i < $za->numFiles; $i++) { $stat = $za->statIndex($i); print_r(basename($stat['name']) . PHP_EOL); } ?>
However, I have not managed to do the same for .7z files. Haven’t tested .rar and .tar, but will need them as well.
原文:https://stackoverflow.com/questions/38510771
最满意答案
如果你
require
两次相同的文件,它将被加载和评估一次。 另一方面,每次加载和评估文件。 实际文件名的解析方式也有所不同( 谢谢,Saurabh )。这实际上意味着什么?
假设我们有一个库
foo
# foo.rb class Foo def bar puts 'bar' end def quux puts 'quux' end end
然后我们有一个文件可以做一些非幂等的操作。 说,没有定义一种方法
# mod.rb class Foo undef :bar end
那么,如果我们
require
mod.rb两次,没有什么不好的事情发生。bar
成功未定义。# main.rb require './foo' Foo.instance_methods(false) # => [:bar, :quux] require './mod' require './mod' Foo.instance_methods(false) # => [:quux]
但是如果我们
load
mod.rb两次,那么第二个undef
操作将会失败,因为方法已经消失了:# main.rb require './foo' Foo.instance_methods(false) # => [:bar, :quux] load 'mod.rb' load 'mod.rb' Foo.instance_methods(false) # => # ~> mod.rb:2:in `<class:Foo>': undefined method `bar' for class `Foo' (NameError) # ~> from mod.rb:1:in `<top (required)>' # ~> from -:6:in `load' # ~> from -:6:in `<main>'
require
没有错误,因为在这种情况下,undef
只会发生一次。 诚然,这个例子很有意思,但我希望它能说明这一点。If you
require
the same file twice, it will be loaded and evaluated only once.load
, on the other hand, loads and evaluates the file every time. There are also differences in how actual filename is resolved (thanks, Saurabh).What does this mean practically?
Let's say we have a library
foo
# foo.rb class Foo def bar puts 'bar' end def quux puts 'quux' end end
Then we have a file which makes some non-idempotent operations. Say, undefines a method
# mod.rb class Foo undef :bar end
Then, if we
require
mod.rb twice, nothing bad happens.bar
gets successfully undefined.# main.rb require './foo' Foo.instance_methods(false) # => [:bar, :quux] require './mod' require './mod' Foo.instance_methods(false) # => [:quux]
But if we
load
mod.rb twice, then secondundef
operation will fail, because method is already gone:# main.rb require './foo' Foo.instance_methods(false) # => [:bar, :quux] load 'mod.rb' load 'mod.rb' Foo.instance_methods(false) # => # ~> mod.rb:2:in `<class:Foo>': undefined method `bar' for class `Foo' (NameError) # ~> from mod.rb:1:in `<top (required)>' # ~> from -:6:in `load' # ~> from -:6:in `<main>'
There's no error with
require
because in that caseundef
happens only once. Granted, this example is quite contrived, but I hope it illustrates the point.
相关问答
更多-
如果你require两次相同的文件,它将被加载和评估一次。 另一方面,每次加载和评估文件。 实际文件名的解析方式也有所不同( 谢谢,Saurabh )。 这实际上意味着什么? 假设我们有一个库foo # foo.rb class Foo def bar puts 'bar' end def quux puts 'quux' end end 然后我们有一个文件可以做一些非幂等的操作。 说,没有定义一种方法 # mod.rb class Foo undef :bar e ...
-
Ruby中的include和require有什么区别?(What is the difference between include and require in Ruby?)[2023-03-09]
Ruby中“include”和“require”有什么区别? 回答: 包含和需求方法做得非常不同。 require方法在大多数其他编程语言中包含哪些功能:运行另一个文件。 它还跟踪您以前需要的,不需要相同的文件两次。 要运行另一个文件没有这个添加的功能,你可以使用load方法。 include方法从另一个模块获取所有方法,并将它们包含在当前模块中。 这是一个语言级的东西,而不是像require那样的文件级的东西。 include方法是使用其他模块(通常称为混合)“扩展”类的主要方式。 例如,如果您的类定义方 ... -
日常工作中没有太多的事情。 但是,根据这两个函数的文档(通过在函数名称之前放置并打入enter来访问), require在函数内部使用,因为它会输出警告,并且如果未找到该包,则继续执行,而library会引发错误。 There's not much of one in everyday work. However, according to the documentation for both functions (accessed by putting a ? before the function na ...
-
1.要求 你是对的,你想要的默认值几乎总是require (与provide配对)。 这些表格与Racket的modules密切相关,使您可以更轻松地确定哪些变量应该在哪些文件中作用。 例如,以下文件定义了三个变量,但只导出2。 #lang racket ; a.rkt (provide b c) (define a 1) (define b 2) (define c 3) 根据Racket样式指南 ,理想情况下,提供应该是#lang之后文件中的第一个表单,以便您可以轻松地告诉模块提供的内容。 在某些 ...
-
require()和new require()之间有什么区别?(What is the difference between require() and new require()?)[2023-02-02]
首先:不要将new require("...") (调用require作为构造函数)与new (require("..."))混淆(它调用require调用的返回值作为构造函数) 。 你正在做第一个。 require不打算作为new的构造函数调用。 那不是Node-idiomatic,而且通常很奇怪。 您不应该这样做,因为它会降低代码的可读性。 未来的读者可能很容易将其误认为是new (require("...")) (即,在由 require 返回的构造函数上调用new ),因为正常的Node样式不使用n ... -
require在所有定义的搜索路径中搜索库,并将.rb或.so追加到您输入的文件名。 它还确保只包含一个库。 因此,如果您的应用程序需要库A和B和库B库库A,A只能加载一次。 在load您需要添加库的全名,并在每次调用load ,即使它已经在内存中。 require searches for the library in all the defined search paths and also appends .rb or .so to the file name you enter. It also m ...
-
使用和需求之间的区别(difference between use and require)[2023-11-29]
require加载libs(尚未加载), use同样的加上它引用它们的命名空间与clojure.core/refer (所以你也可以使用:exclude等像clojure.core/refer )。 两者都推荐用于ns而不是直接使用。 require loads libs (that aren't already loaded), use does the same plus it refers to their namespaces with clojure.core/refer (so you also ... -
在Ruby中require_relative和require之间有什么区别?(What is the difference between require_relative and require in Ruby?)[2022-03-07]
只看文档 : require_relative通过允许您加载与包含require_relative语句的文件相对的文件来补充内置方法的require 。 例如,如果您在“测试”目录中有单元测试类,并且在测试“test / data”目录下有数据,那么您可能会在测试用例中使用这样的行: require_relative "data/customer_data_1" Just look at the docs: require_relative complements the builtin method r ... -
我只想向你解释这两个语句,这两个语句不是rails中的函数。 Rails使用缓存来缓存先前加载的文件。 如果你的缓存是真的,那么它使用require,否则它使用load语句独立加载缓存。 我只是想说,需要用于缓存已经加载的文件,加载总是命中服务器来加载文件。 I simply like to explain you about those two statements , those two are not functions in rails . Rails uses cache to cache the ...
-
我在Debian系统上遇到了类似的问题,我猜你正在使用它或者基于系统,因为你正在使用替代品,问题是我通过apt-get安装了rubygems然后安装通过源代码更新的版本。 把事情弄好了。 Debian和rubygems团队之间存在某种“宗教”战争,因此在Debian上安装rubygems更容易。 我没有解释发生了什么。 :( I've had similar problems on a Debian system, which I'm guessing you're using or a system ba ...