Linux:解压缩包含同名文件的存档

发布于 2024-08-07 03:08:12 字数 312 浏览 5 评论 0原文

我收到了一个 zip 文件,其中包含 40 个同名文件。 我想将每个文件提取到一个单独的文件夹或使用不同的名称(file1、file2 等)提取每个文件。

有没有办法使用标准 Linux 工具自动执行此操作? man unzip 的检查没有发现任何可以帮助我的东西。 zipsplit 似乎也不允许任意分割 zip 文件(我试图将 zip 分割成 40 个档案,每个档案包含一个文件)。

目前我正在单独(r)命名我的文件。对于 40 个文件的存档来说,这并不是什么大问题,但显然是不可扩展的。

有人有一个好的、简单的方法吗?比什么都好奇。

谢谢。

I was sent a zip file containing 40 files with the same name.
I wanted to extract each of these files to a seperate folder OR extract each file with a different name (file1, file2, etc).

Is there a way to do this automatically with standard linux tools? A check of man unzip revealed nothing that could help me. zipsplit also does not seem to allow an arbitrary splitting of zip files (I was trying to split the zip into 40 archives, each containing one file).

At the moment I am (r)enaming my files individually. This is not so much of a problem with a 40 file archive, but is obviously unscalable.

Anyone have a nice, simple way of doing this? More curious than anything else.

Thanks.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏爱自由 2024-08-14 03:08:12

假设目前不存在这样的工具,那么用 python 编写一个应该很容易。 Python 有一个 zipfile 模块 应该足够了。

像这样的东西(也许,未经测试):

#!/usr/bin/env python

import os
import sys
import zipfile

count = 0

z = zipfile.ZipFile(sys.argv[1],"r")

for info in z.infolist():
    directory = str(count)
    os.makedirs(directory)
    z.extract(info,directory)
    count += 1

z.close()

Assuming that no such tool currently exists, then it should be quite easy to write one in python. Python has a zipfile module that should be sufficient.

Something like this (maybe, untested):

#!/usr/bin/env python

import os
import sys
import zipfile

count = 0

z = zipfile.ZipFile(sys.argv[1],"r")

for info in z.infolist():
    directory = str(count)
    os.makedirs(directory)
    z.extract(info,directory)
    count += 1

z.close()
十雾 2024-08-14 03:08:12

我知道这已经有几年了,但是上面的答案并没有解决我的特定问题,所以我想我应该继续发布一个对我有用的解决方案。

无需编写脚本,您只需使用命令行输入即可与解压缩工具文本界面进行交互。也就是说,当您在命令行中键入以下内容时:

unzip file.zip

并且它包含同名文件,它将提示您:

replace sameName.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:

如果您想手动执行此操作,您将键入“r”,然后在下一个提示符下:

new name:

您只需输入新文件名即可。

要自动执行此操作,只需创建一个包含对这些提示的响应的文本文件,并将其用作解压缩的输入,如下所示。

r
sameName_1.txt
r
sameName_2.txt
...

使用您最喜欢的脚本语言可以很容易地生成它。将其另存为 unzip_input.txt,然后将其用作解压缩的输入,如下所示:

unzip < unzip_input.txt

对我来说,这比尝试让 Perl 或 Python 提取模块按我需要的方式工作更让人头疼。希望这对某人有帮助...

I know this is a couple years old, but the answers above did not solve my particular problem here so I thought I should go ahead and post a solution that worked for me.

Without scripting, you can just use command line input to interact with the unzip tools text interface. That is, when you type this at the command line:

unzip file.zip

and it contains files of the same name, it will prompt you with:

replace sameName.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you wanted to do this by hand, you would type "r", and then at the next prompt:

new name:

you would just type the new file name.

To automate this, simply create a text file with the responses to these prompts and use it as the input to unzip, as follows.

r
sameName_1.txt
r
sameName_2.txt
...

That is generated pretty easily using your favorite scripting language. Save it as unzip_input.txt and then use it as input to unzip like this:

unzip < unzip_input.txt

For me, this was less of a headache than trying to get the Perl or Python extraction modules working the way I needed. Hope this helps someone...

回心转意 2024-08-14 03:08:12

这是一个Linux脚本版本,

在这种情况下,834733991_T_ONTIME.csv是每个zip文件中相同的文件名,“$count”后面的.csv只需与您想要的文件类型交换

#!/bin/bash

count=0

for a in *.zip
do
    unzip -q "$a"
    mv 834733991_T_ONTIME.csv "$count".csv
    count=$(($count+1))
done`

here is a linux script version

in this case the 834733991_T_ONTIME.csv is the name of the file that is the same inside every zip file, and the .csv after "$count" simply has to be swapped with the file type you want

#!/bin/bash

count=0

for a in *.zip
do
    unzip -q "$a"
    mv 834733991_T_ONTIME.csv "$count".csv
    count=$(($count+1))
done`
旧故 2024-08-14 03:08:12

该线程很旧,但仍有改进的空间。就我个人而言,我更喜欢 bash 中的以下一行,这是

unzipd ()
{
    unzip -d "${1%.*}" "$1"
}

一种很好、干净、简单的方法来删除扩展并使用

This thread is old but there is still room for improvement. Personally I prefer the following one-liner in bash

unzipd ()
{
    unzip -d "${1%.*}" "$1"
}

Nice, clean, and simple way to remove the extension and use the

梦在夏天 2024-08-14 03:08:12

使用 unzip -B file.zip 为我解决了这个问题。如果文件已存在,它会创建一个后缀为 ~ 的备份文件。

例如:

$ rm *.xml
$ unzip -B bogus.zip
Archive:  bogus.zip
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
$ ls -l
-rw-rw-r--  1 user user    1161 Dec 20 20:03 bogus.zip
-rw-rw-r--  1 user user    1501 Dec 16 14:34 foo.xml
-rw-rw-r--  1 user user    1520 Dec 16 14:45 foo.xml~
-rw-rw-r--  1 user user    1501 Dec 16 14:47 foo.xml~1
-rw-rw-r--  1 user user    1520 Dec 16 14:53 foo.xml~2
-rw-rw-r--  1 user user    1520 Dec 16 14:54 foo.xml~3

注意:-B 选项不会出现在 unzip --help 中,但在手册页中提到:https://manpages.org/unzip#options

Using unzip -B file.zip did the trick for me. It creates a backup file suffixed with ~<number> in case the file already exists.

For example:

$ rm *.xml
$ unzip -B bogus.zip
Archive:  bogus.zip
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
$ ls -l
-rw-rw-r--  1 user user    1161 Dec 20 20:03 bogus.zip
-rw-rw-r--  1 user user    1501 Dec 16 14:34 foo.xml
-rw-rw-r--  1 user user    1520 Dec 16 14:45 foo.xml~
-rw-rw-r--  1 user user    1501 Dec 16 14:47 foo.xml~1
-rw-rw-r--  1 user user    1520 Dec 16 14:53 foo.xml~2
-rw-rw-r--  1 user user    1520 Dec 16 14:54 foo.xml~3

Note: the -B option does not show up in unzip --help, but is mentioned in the man pages: https://manpages.org/unzip#options

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文