首页 \ 问答 \ 使用PHP列出.7z,.rar和.tar档案中的文件(List files in .7z, .rar and .tar archives using PHP)

使用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
更新时间:2023-07-05 14:07

最满意答案

如果你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 second undef 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 case undef 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”有什么区别? 回答: 包含和需求方法做得非常不同。 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之后文件中的第一个表单,以便您可以轻松地告诉模块提供的内容。 在某些 ...
  • 首先:不要将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 ...
  • 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 ...
  • 只看文档 : 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 ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)