-->

DotNetZip zipping files with Arabic names

2019-09-06 17:02发布

问题:

When creating a zip file out of many Arabic named files, I have as prompted in DotNetZip's FAQ changed the code page to the following:

                  Using zip As New ZipFile()
                zip.AddDirectoryByName("Files")
                zip.AlternateEncoding = Encoding.UTF8
                zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always
                Dim row As Integer
                For row = 0 To ds.Tables("d").Rows.Count - 1

                    fileToDownload = Server.MapPath("~/.../Resources/Attachments/" + ds.Tables("d").Rows(row).Item(1).ToString)
                    zip.AddFile(fileToDownload, "Files")

                Next
                Response.Clear()
                Response.BufferOutput = False
                Dim zipName As String = [String].Format(gvRow.Cells(8).Text.Trim + ".zip")
                Response.ContentType = "application/zip"
                Response.AddHeader("content-disposition", "attachment; filename=" + zipName)
                zip.Save(Response.OutputStream)
                Response.[End]()

            End Using

I have used several listed Arabic encoding codes, but most of them produce '???' whereas this one produces names as the following: '¦ßs-¦ µ+++ ¦ß+pß.docx'

What is the correct code to be used? Or am I missing something?

回答1:

Use UTF8 Encoding and pass it as parameter to the constructor:

IO.File.Delete("D:/testZip.zip")
Using zip As New Ionic.Zip.ZipFile(Encoding.UTF8)
      zip.AddDirectory("d:/out")
      zip.Save("D:/testZip.zip")
End Using

this code works with me with Arabic file names (windows 7).
EDIT #1 :
you must force DotNetZip to use the specified encoding by using Always option instead of AsNesseary :

 IO.File.Delete("D:/testZip.zip")
  Using zip As New Ionic.Zip.ZipFile()
            zip.AlternateEncoding = Encoding.UTF8
            zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always
            zip.AddDirectory("d:/out")
            zip.Save("D:/testZip.zip")
  End Using

EDIT #2 :
based on your comment, I think your operating system does not support Arabic UI,for windows 7 professional 32 bit, go to Control Panel -> Region and Language -> Administrative [tab] -> click "change System locale" button -> choose "Arabic Egypt" for Example -> OK -> OK (restart computer is needed) , Don't worry, the language of windows 7 still English.
EDIT #3 :
As I mentioned in EDIT #2, your system must support Arabic UI, for example, create a file called (ملف جديد) then add it to archive using WinZip or Winrar, then open the generated archive, if you can read file names correctly , then try to use one of the following encodings in your code :

Encoding.Unicode
Encoding.UTF7
Encoding.UTF8
Encoding.UTF32

If you are unable to read Arabic file names inside the generated archive, you must configure your system to support Arabic UI.
Also, please use the following order for these lines, put the Encoding, then add files or folders :

zip.AlternateEncoding = Encoding.UTF8
zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always
zip.AddDirectoryByName("Files")


回答2:

After using what seemed like a myriad of trials using code pages, simply replacing this:

                zip.AlternateEncoding = Encoding.UTF8

with this:

                zip.AlternateEncoding = Encoding.GetEncoding(720)

worked.