如何使用命令行工具“unzip”仅抓取某个文件夹?

如何使用命令行工具“unzip”仅抓取某个文件夹?

我有一个 .jar 文件,可以使用命令行工具“unzip”打开,它有这个选项,-p因此您可以抓取某些文件。但是如何使用文件夹执行此操作?以下是此 jar 文件的结构。

My.jar
|-folder
|   |-Content
|        |-My Files
|   |-First Folder
|        |-Second Folder
|             |-Files
|-image.png
|-foo.class
|-bar.class

我想要抓取的文件夹“folder”也包含比“我的文件”和“文件”多得多的文件。

我尝试过的命令:unzip dir/to\ a\ folder/My.jar -d dir/to\ a/desired\ folder -p folder/ | image.png

没有其他文件:unzip dir/to\ a\ folder/My.jar -d dir/to\ a/desired\ folder -p folder/

我尝试使用jar命令来执行此操作,这似乎有效,但我特别想使用 来执行此操作unzip

我得到的错误是:

警告:文件名不匹配:-p
警告:文件名不匹配:assets/,

解压帮助菜单我有: 在 macOS 10.13.6 上解压

答案1

很好,您正在运行的只是标准信息压缩unzip

您使用的选项-p未达到预期效果。以下是 的条目的手册页-p

   -p     extract files to pipe (stdout).  Nothing but the  file  data  is
          sent  to  stdout,  and  the files are always extracted in binary
          format, just as they are stored (no conversions).

让我们来看一个例子。首先创建一个类似于你的目录树

data
`-- folder
    `-- Content
        |-- FirstFolder
        |   `-- SecondFolder
        |       `-- Files
        |           |-- f1
        |           |-- f2
        |           `-- f3
        `-- MyFiles
            |-- a1
            |-- a2
            `-- a3

把它塞进拉链里

$ zip -r try.zip data
$ unzip -l data.zip
Archive:  try.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  04-22-2020 08:59   data/
        0  04-22-2020 08:59   data/file1
        0  04-22-2020 08:59   data/file2
        0  04-22-2020 08:56   data/folder/
        0  04-22-2020 08:57   data/folder/Content/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f2
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f1
        0  04-22-2020 08:57   data/folder/Content/FirstFolder/SecondFolder/Files/f3
        0  04-22-2020 08:57   data/folder/Content/MyFiles/
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a3
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a1
        0  04-22-2020 08:57   data/folder/Content/MyFiles/a2
        0  04-22-2020 08:59   data/file3
---------                     -------
        0                     16 files

如果只想提取folder子树,则需要提供unzip与子树匹配的通配符。在本例中data/folder/*可以解决问题。

$ unzip -d output try.zip 'data/folder/*' 
Archive:  try.zip
   creating: output/data/folder/
   creating: output/data/folder/Content/
   creating: output/data/folder/Content/FirstFolder/
   creating: output/data/folder/Content/FirstFolder/SecondFolder/
   creating: output/data/folder/Content/FirstFolder/SecondFolder/Files/
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f2  
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f1  
 extracting: output/data/folder/Content/FirstFolder/SecondFolder/Files/f3  
   creating: output/data/folder/Content/MyFiles/
 extracting: output/data/folder/Content/MyFiles/a3  
 extracting: output/data/folder/Content/MyFiles/a1  
 extracting: output/data/folder/Content/MyFiles/a2  

相关内容