ESSENCE
How to write a zip file using libarchive in C ++, so that the path names will be encoded in UTF-8? With UTF-8 path names, special characters will be correctly decoded when using OS X / Linux / Windows 8/7-Zip / WinZip.
DETAILS
I am trying to write a zip archive using libarchive, compiling with Visual C ++ 2013 on Windows.
I would like to be able to add files with non-ASCII characters (for example, äöü.txt) to the zip archive.
There are four functions for setting the path header in libarchive:
void archive_entry_set_pathname(struct archive_entry *, const char *); void archive_entry_copy_pathname(struct archive_entry *, const char *); void archive_entry_copy_pathname_w(struct archive_entry *, const wchar_t *); int archive_entry_update_pathname_utf8(struct archive_entry *, const char *);
Unfortunately, none of them seem to be working.
In particular, I tried:
const char* myUtf8Str = ... archive_entry_update_pathname_utf8(entry, myUtf8Str);
and
const wchar_t* myUtf16Str = ... archive_entry_copy_pathname_w(entry, myUtf16Str);
In both cases, the resulting zip archive does not show the file names correctly in both Windows Explorer and 7-Zip.
I am sure my input lines are encoded correctly, as I convert them from Qt QString instances that work fine in other parts of my code:
const char* myUtf8Str = filename.toUtf8().constData(); const wchar_t* myUtf16Str = filename.toStdWString().c_str();
For example, this works even for another libarchive call when creating a zip file:
archive_write_open_filename_w(archive, zipFile.toStdWString().c_str());
I also tried changing the libarchive options as suggested by this example :
archive_write_set_options(a, "hdrcharset=UTF-8");
But this call fails, so I guess I need to install another option, but my ideas are running out ...
UPDATE 2
I read about the zip format. It allows you to write file names to UTF-8, so that OS X / Linux / Windows 8/7-Zip / WinZip will always decode them correctly, see, for example, here .
This is what I want to achieve with libarchive, i.e. I would like to pass its encoded UTF-8 pathname and save it in a zip file without any conversion.
I added the "set locale" approach as a (unsatisfactory) answer.