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

时间:2023-01-25 11:34:29

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).

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

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).

有没有办法用标准的linux工具自动完成?一个男人解压缩的检查显示没有任何可以帮助我。 zipsplit似乎也不允许任意分割zip文件(我试图将zip分成40个档案,每个档案包含一个文件)。

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.

目前我(r)单独命名我的文件。对于40文件存档而言,这不是一个问题,但显然是不可扩展的。

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

任何人都有一个很好的,简单的方法吗?比其他任何东西都更好奇。

Thanks.

3 个解决方案

#1


6  

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.

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

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()

#2


3  

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:

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

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_input.txt,然后将其用作输入以解压缩,如下所示:

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...

对我来说,这并不像试图让Perl或Python提取模块按照我需要的方式工作。希望这有助于某人......

#3


2  

here is a linux script version

这是一个linux脚本版本

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

在这种情况下,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`

#1


6  

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.

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

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()

#2


3  

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:

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

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_input.txt,然后将其用作输入以解压缩,如下所示:

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...

对我来说,这并不像试图让Perl或Python提取模块按照我需要的方式工作。希望这有助于某人......

#3


2  

here is a linux script version

这是一个linux脚本版本

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

在这种情况下,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`