2

I am trying to zip a file using C# (.net 4.6). Here is my code:

string targetLoc = @"C:\path\to\target\zip";
string sourceFile = @"C:\path\to\source\file\ファイル名.csv";
ZipArchive zip = ZipFile.Open(Path.Combine(targetLoc, "ZipFile.zip"), ZipArchiveMode.Create, Encoding.UTF8);

using (zip)
    {
         string fileName = Path.GetFileName(sourceFile);
         zip.CreateEntryFromFile(sourceFile, fileName);
    }

The problem is, the file name contained in the resulted zip file became this: 繝輔ぃ繧、繝ォ蜷・csv. I tried changing the encoding of the zip (in the ZipArchive zip = ZipFile.Open(Path.Combine(targetLoc, "ZipFile.zip"), ZipArchiveMode.Update, Encoding.UTF8);), but I got ArgumentException saying that the encoding is not supported.

Is there any workaround for this? As much as possible I do not want to use third party library.

note1: the zip file did not previously exist.

note2: I am using win7 Pro, Japanese version (not possible to change language)

Thank you in advance!

Leonard AB
  • 1,479
  • 1
  • 19
  • 30
  • It should work, if you are creating a new archive. I don't believe you can't change the encoding in an existing archive. See [my answer here](https://stackoverflow.com/a/32443735) for more details about how encoding is handled for .zip archives. The question above is not really answerable, as you're vague about what "not encoded properly" means and haven't provided a [mcve]. Please be _specific_. If you believe the file name isn't encoded properly, explain what _exactly_ leads you to believe that. – Peter Duniho Feb 14 '18 at 00:14
  • thanks for the comment, I edited the question. – Leonard AB Feb 14 '18 at 00:35
  • Replace Encoding.UTF8 with Encoding.Unicode – jdweng Feb 14 '18 at 00:41
  • @jdweng as I mentioned above, I tried that and got ArgumentException stating that the encoding is not supported. – Leonard AB Feb 14 '18 at 00:47
  • Read your posting again. I think you made a typo. – jdweng Feb 14 '18 at 00:51
  • @jdweng hmm, I am not sure where. Do you mind to point that out? – Leonard AB Feb 14 '18 at 00:56
  • 1
    @jdweng: UTF16 (i.e. Encoding.Unicode) is not a supported encoding for .zip archives. – Peter Duniho Feb 14 '18 at 00:59
  • @LeonardAB: you've edited the question, but still not provided a [mcve]. Because we don't know the state of the .zip archive you started with, it's not possible to know how to exactly reproduce the scenario you are seeing. And as I noted, if the original .zip archive isn't using UTF8 as the encoding, you're not likely to be able to change that later. – Peter Duniho Feb 14 '18 at 01:00
  • @PeterDuniho the zip file did not previously exist. To make it clearer I changed `ZipArchiveMode` to `Create` in the question above – Leonard AB Feb 14 '18 at 01:06
  • 1
    Nevertheless, I am unable to reproduce using the code you posted. When I run that code, I get an entry with the expected file name of `"ファイル名.csv"`, when inspecting either via re-opening as a `ZipArchive` or viewing in Windows Explorer. Either you have not posted the exact code you're using, you are using an old version of .NET that doesn't support UTF8 entry names, or you are using some other tool to open the archive and view it which does not support UTF8 entry names. – Peter Duniho Feb 14 '18 at 01:17
  • @PeterDuniho thank you for trying. It is the exact code (except for the path), and I am using only Windows Explorer to view the archive. My Windows language is Japanese. Do you think it has something to with that? – Leonard AB Feb 14 '18 at 04:21
  • 1
    _"My Windows language is Japanese. Do you think it has something to with that?"_ -- it could, if there's a bug in Windows. You can always try switching your Windows language to English, to see if that fixes it. Also, you should check to see what .NET says the entry name is. If .NET reports the entry name as expected (which I believe it will), and the only place it appears wrong is in Windows Explorer, then I'd say clearly there's a bug in Windows Explorer. What version Windows? Note that prior to Windows 8, it was known that it doesn't support UTF8 encoding in .zip archives. – Peter Duniho Feb 14 '18 at 04:28
  • @PeterDuniho Thanks! I tried to read the name of the file inside the archive programmatically, and yes you are correct, the entry name is `"ファイル名.csv"` as expected. I am using win7 so maybe that's the problem. (but if I zip a file directly in windows explorer, the problem does not exist though). Problem not solved but thanks again for sharing your knowledge. – Leonard AB Feb 14 '18 at 06:11
  • _"if I zip a file directly in windows explorer, the problem does not exist though"_ -- if you .zip the file in Windows Explorer in Windows 7, it will use your current code page, not UTF8, to encode the file name. Of course, then you'll have trouble reading it back out in .NET (but you can work around that, as long as you know the code-page...see the answer I posted in the other question, and linked above). – Peter Duniho Feb 14 '18 at 06:23

0 Answers0