Reputation: 1196
I've been using DotNetZip for a while but just ran into a problem. I have some text (not the filename but the actual text contents) which includes non-Latin characters (specifically Vietnamese). When I use DotNetZip to zip up this content, every time I unzip it, the text is garbled (changed in a bad way).
Dim strOriginal As String = "Đinh Quỳnh Ngô Nguyễn Phạm Tuấn Vũ Phương Ngọc Châu Trần Thị Ngọc Hồng Hiền Tô"
Using zip As New Ionic.Zip.ZipFile()
zip.AlternateEncoding = System.Text.Encoding.Unicode
zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.Always
zip.AddEntry("data", strOriginal)
zip.Save("test.zip")
End Using
Dim strContents As String
Using zip As New Ionic.Zip.ZipFile("test.zip")
Using ms As New System.IO.MemoryStream()
zip.Entries(0).Extract(ms)
ms.Position = 0
Using sr As New System.IO.StreamReader(ms)
strContents = sr.ReadToEnd
sr.Close()
End Using
ms.Close()
End Using
End Using
If strOriginal = strContents Then
MsgBox("Happy")
Else
MsgBox("Sad")
End If
I've tried the following Encoding options: UTF7, UTF8, Unicode, UTF32, and BigEndianUnicode. All produce bad results.
Is this a bug or is my implementation off somehow?
In case it matters, I'm using VB.Net 2010, Win7 x64, .Net 3.5, and DotNetZip 1.9.1.8.
Upvotes: 0
Views: 743
Reputation: 1196
I found my own answer. I'll post it here in case anyone in the future is looking for it.
Dim strOriginal As String = "Đinh Quỳnh Ngô Nguyễn Phạm Tuấn Vũ Phương Ngọc Châu Trần Thị Ngọc Hồng Hiền Tô"
Using zip As New Ionic.Zip.ZipFile()
Dim byteArray() As Byte = System.Text.Encoding.Unicode.GetBytes(strOriginal)
zip.AddEntry("data", byteArray)
zip.Save("test.zip")
End Using
Dim strContents As String
Using zip As New Ionic.Zip.ZipFile("test.zip")
Using ms As New System.IO.MemoryStream()
zip.Entries(0).Extract(ms)
ms.Position = 0
Using sr As New System.IO.StreamReader(ms, System.Text.Encoding.Unicode)
strContents = sr.ReadToEnd
sr.Close()
End Using
ms.Close()
End Using
End Using
If strOriginal = strContents Then
MsgBox("Happy")
Else
MsgBox("Sad")
End If
Upvotes: 1