Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Currently, tar -zcf arch.tgz files/* encodes filenames in UTF, so Windows users see all characters spoiled in filenames which are not english, and can do nothing with it.

zip -qq -r arch.zip files/* has the same behavior.

How can I create a zip / tgz archive so when Windows users extract it will have all filenames encoded properly?

share|improve this question
add comment

7 Answers

up vote 16 down vote accepted

Currently, tar encodes filenames in UTF

Actually tar doesn't encode/decode filenames at all, It simply copies them out of the filesystem as-is. If your locale is UTF-8-based (as in many modern Linux distros), that'll be UTF-8. Unfortunately the system codepage of a Windows box is never UTF-8, so the names will always be mangled except on tools such as WinRAR that allow the charset used to be changed.

So it is impossible to create a ZIP file with non-ASCII filenames that work across different countries' releases of Windows and their built-in compressed folder support.

It is a shortcoming of the tar and zip formats that there is no fixed or supplied encoding information, so non-ASCII characters will always been non-portable. If you need a non-ASCII archive format you'll have to use one of the newer formats, such as recent 7z or rar. Unfortunately these are still wonky; in 7zip you need the -mcu switch, and rar still won't use UTF-8 unless it detects characters not in the codepage.

Basically it's a horrible mess and if you can avoid distributing archives containing filenames with non-ASCII characters you'll be much better off.

share|improve this answer
    
Great, thanks! Unfortunately, most users know nothing about 7z, and rar is proprietary :( –  kolypto Oct 26 '09 at 0:29
    
Yeah, it's a problem. ZIP is by far the most usable solution for users, as all modern OSes have nice native UI support for it. Unfortunately the charset problem is not really solvable today in ZIP (and even in other archive formats it's still troublesome). –  bobince Oct 28 '09 at 19:51
add comment

Here is a simple Python script that I've written to unpack tar files from UNIX on Windows:

import tarfile

archive_name = "archive_name.tar"

def recover(name):
    return unicode(name, 'utf-8')

tar = tarfile.open(name=archive_name, mode='r', bufsize=16*1024)
updated = []
for m in tar.getmembers():
    m.name = recover(m.name)
    updated.append(m)

tar.extractall(members=updated)
tar.close()
share|improve this answer
    
Awesome! this script helped me convert a EUC-JP encoded tar file that was created on an old Solaris server. –  wm_eddie Dec 9 '10 at 10:19
    
You are welcome ;) –  Alexei Osipov Dec 10 '10 at 22:22
    
Excellent, thank you! :-) –  Christian Davén Apr 8 '13 at 8:35
add comment

I believe you're running into problems with the Zip container format itself. Tar may be suffering from the same problem.

Use the 7zip (.7z) or RAR (.rar) archive formats instead. Both are available for Windows and Linux; the p7zip software handles both formats.

I just tested creating .7z, .rar, .zip, and .tar files on both WinXP and Debian 5, and the .7z and .rar files store/restore filenames correctly while the .zip and .tar files don't. It doesn't matter which system is used to create the test archive.

share|improve this answer
add comment

The problem, using in Linux the default tar (GNU tar), is solved... adding the --format=posix parameter when creating the file.

For example:
tar --format=posix -cf

In Windows, to extract the files, I use bsdtar.

In https://lists.gnu.org/archive/html/bug-tar/2005-02/msg00018.html it is written (since 2005!!):

> I read something in the ChangeLog about UTF-8 being supported. What does
> this mean?
> I found no way to create an archive that would be interchangeable
> between different locales.

When creating archives in POSIX.1-2001 format (tar --format=posix or --format=pax), tar converts file names from the current locales to UTF-8 and then stores them in archive. When extracting, the reverse operation is performed.

P.S. Instead of typing --format=posix you can type -H pax, which is shorter.

share|improve this answer
add comment

POSIX-1.2001 specified how TAR uses UTF-8.

As of 2007, changelog version 6.3.0 in the PKZIP APPNOTE.TXT (http://www.pkware.com/documents/casestudies/APPNOTE.TXT) specified how ZIP uses UTF-8.

It's only which tools support these standards properly, that remains an open question.

share|improve this answer
add comment

I had problems with unpacking tar and zip files I receive from Windows users. While I do not answer the question "how to create the archive which will work", the scripts below help to unpack tar and zip files correctly regardless the original OS.

WARNING: one has to tune the source encoding manually (cp1251, cp866 in examples below). Commandline options may be a good solution in a future.

Tar:

#!/usr/bin/env python

import tarfile
import codecs
import sys

def recover(name):
    return codecs.decode(name, 'cp1251')

for tar_filename in sys.argv[1:]:
    tar = tarfile.open(name=tar_filename, mode='r', bufsize=16*1024)
    updated = []
    for m in tar.getmembers():
        m.name = recover(m.name)
        updated.append(m)
    tar.extractall(members=updated)
    tar.close()

Zip:

#!/usr/bin/env python

import zipfile
import os
import codecs
import sys

def recover(name):
    return codecs.decode(name, 'cp866')

for filename in sys.argv[1:]:
    archive = zipfile.ZipFile(filename, 'r')
    infolist = archive.infolist()
    for i in infolist:
        f = recover(i.filename)
        print f
        if f.endswith("/"):
            os.makedirs(os.path.dirname(f))
        else:
            open(f, 'w').write(archive.read(i))
    archive.close()
share|improve this answer
add comment

As far as I know, Windows NT series (XP, Vista, others) use Unicode for storing the file names. So I'm guessing it's a problem with the Windows' archiver... which one are you using?

share|improve this answer
    
i've seen this behavior with old WinRAR and new 7zip. i'm not entirely sure it's not a problem with the unix archivers. –  quack quixote Oct 25 '09 at 16:37
    
@~quack, this is a problem with tgz,zip,... formats themselves :) they can't declare filenames' encoding, and all *nix OSes assume it's UTF, while Windows assumes it's .. something else :) –  kolypto Oct 25 '09 at 19:53
    
@o_O Tync: you mean Linux has backed off from its' "meaningless bytes" stance? –  SamB May 29 '10 at 16:58
    
@SamB: No, but most distros default to using UTF-8 locales (en_US.utf8) and this causes UTF-8 to be used in filenames. –  grawity May 29 '10 at 17:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.