Is it possible to rename files in a zip archive before extracting?

4

1

I pulled a zip file from the web which was packed on a system where filenames were encoded in a format my unzip program fails to read. Perhaps the file-system does not support those weird characters, or the shell. I'm not entirely sure.

Since I don't really care about the filename an acceptable solution would be to rename the file, or write the contents to a file named something else than the name specified in the archive. Is that possible with a common command line zip utility? If so, how. Other suggestions on what may be wrong (perhaps it's not the zip utility) are also welcome.

The symptoms are as follows:


$ unzip -l 3688232.zip ; unzip 3688232.zip 
Archive:  3688232.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
   107937  10-24-2012 01:00   [-?M+?V+?e-?] Le Gout Des autres - Agn+?s Jaoui - 2000 (sep subs).srt
     6354  10-24-2012 01:00   le.gout.des.autres.(3688232).nfo
---------                     -------
   114291                     2 files
Archive:  3688232.zip
error:  cannot create [-?M+?V+?e-?] Le Gout Des autres - Agn+?s Jaoui - 2000 (sep subs).srt
        Invalid or incomplete multibyte or wide character

$ unzip --help 
UnZip 6.00 of 20 April 2009, by Info-ZIP.  Maintained by C. Spieler.  Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.

Note:
I am not particularly interested in suggestions involving graphical archivers, because I have already successfully extracted the file using `wine WinRAR.exe'.

Ярослав Рахматуллин

Posted 2012-10-23T23:23:11.597

Reputation: 9 076

You can't, with normal tools, rename a file while it's in the ZIP archive. Best you can do is to rename files as you extract them. – Daniel R Hicks – 2013-12-11T23:28:31.673

Though you could, with a slightly abnormal tool (a hex editor) go into the file and "zap" the "odd" characters (replace with "x", eg). It would take some slight knowledge of the ZIP format, but the names are in clear text, so it's not that difficult. You do need to zap them in two places, though, in the general case. – Daniel R Hicks – 2013-12-12T12:00:48.127

Answers

2

Can you try unzip -p file.zip archivedfilename.ext > newfilename.ext?

Karan

Posted 2012-10-23T23:23:11.597

Reputation: 51 857

That concatenates all the files into one. funzip is better for that purpose because you get only the first file on stdout, but you'll run into trouble if the desired file is not "first in the archive". – Ярослав Рахматуллин – 2012-10-23T23:55:37.863

So even if you specify the name of the archived file (with wildcards to deal with the issue of invalid characters), unzip still extracts all the files? – Karan – 2012-10-23T23:59:33.157

I misread your answer tbh. This works: unzip -p 3688232.zip *srt > moo.srt – Ярослав Рахматуллин – 2012-10-24T00:03:45.467

3

I managed to get all the files with Python:

import zipfile
z = zipfile.ZipFile('file.zip')
for i, f in enumerate(z.filelist):
    f.filename = 'file_{0:03}'.format(i)
    z.extract(f)

You will also get some empty files corresponding to the directory entries.

Gaël Ecorchard

Posted 2012-10-23T23:23:11.597

Reputation: 131

1

The GNU Info-Zip package includes a utility called zipnote which allows you to edit file names inside a zip archive as well as manipulate file comments ("notes").

zipnote -w file.zip <<<$'@ oldname\n@=newname'

It seems you have to know the old name exactly; there is no wildcard support.

It also seems you can only rename a single file at a time. I ended up with the following to rename all the files in an archive:

unzip -Z1 file.zip |
awk '{ printf "file%06i %s\n", NR, $1 }' |
while read -r to from; do
    printf '@ %s\n@=%s\n' "$from" "$to" | zipnote -w file.zip
done

(Note the trickery to print the regularized file name "$to" first in the Awk script.)

tripleee

Posted 2012-10-23T23:23:11.597

Reputation: 2 480

0

In a bug filed on unzip, Yannis Tsop (ogiannhs) wrote on 2010-05-25: #12

using 7-zip it can be solved!

eg:

LANG=el_GR.CP737 7z x -oPATH file.zip 
convmv -r --notest -f utf-8 -t CP737 PATH/*

Yury

Posted 2012-10-23T23:23:11.597

Reputation: 11