Fixing Broken Zip Files from Bandcamp - File name too long


I recently downloaded an album from Bandcamp. On trying to extract the zip file using Ubuntu, I got the error "File name too long." This is a known problem with Bandcamp.

If you have a problem with long filenames in zip files, here's how to fix it.

Quick Solution

unzip -p -c whatever.zip "filename.ogg" > shortname.ogg

That will extract a specific file from the zip and then rename it before writing it to disk.

If you don't want to use the whole file name, you can use

unzip -p -c whatever.zip "*a bit of the filename*" > shortname.ogg

More than one file

Amy Pond, from the TV show Doctor Who, is strapped into a chair. She is saying "OK, kid, This is where where it gets complicated"

It is impossible to rename a file inside a zip on Linux - if the name is already too long. Every app will fail if you try to do so.

In order to truncate the filenames, you need to do two things. First, get a list of all the files.

zipinfo -2 example.zip

Then we pipe all of those file names into the above example and rename them.

zipinfo -2 example.zip | while read i; do unzip -p -c example.zip "$i" > "${i:0:127}" ; done;

In this example, I'm using 128 characters as the maximum filename length - your requirements may be different. This also removes the file extension.

So, here's how to extract long filenames from a zip, shorten the name, and preserve the file extension.

zipinfo -2 example.zip | while read i;
do
  long_fname=${i%.*}
  unzip -p -c example.zip "$i" > "${long_fname:0:123}.${i##*.}"
done;

Thanks to ravexina for answering this question.

Leave a Reply

Your email address will not be published. Required fields are marked *