1

I create a zip file and I copy in it a file that contains a serialized list of objects. The file encoding is in UTF8. Then I unzip the file and I try to deserialize it, but I will get this error:

Unexpected character encountered while parsing value: . Path '', line 0, position 0

The problem does not exist if I use ASCII encoding instead of UTF8. But I need to use the UTF8. So I am wondering if the DotNetZip library does not have full support for the UTF8, or maybe I am missing something else.

In order to reproduce the error:

Json library is the one at: http://json.codeplex.com/

The Zip library is the one at: http://dotnetzip.codeplex.com/

Create a simple class "Dog":

public class Dog
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Then use this code (the last line will cause the error):

       var list = new List<Dog>();          
        list.Add(new Dog { FirstName = "Arasd", LastName = "1234123" });
        list.Add(new Dog { FirstName = "fghfgh", LastName = "vbnvbn" });

        var serialized = JsonConvert.SerializeObject(list, Formatting.Indented);


        var zipFile = new ZipFile(@"C:\Users\daviko\Desktop\test.zip");

        using (zipFile)
        {
            zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            zipFile.UpdateEntry("dogs.txt", serialized, UTF8Encoding.UTF8);
            zipFile.Save();
        }

        var readFromZipFile = string.Empty;

        using (var input = new MemoryStream())
        {
            using (zipFile)
            {
                var entry = zipFile["dogs.txt"];
                entry.Extract(input);
            }
            using (var output = new MemoryStream())
            {
                input.CopyTo(output);
                readFromZipFile = new UTF8Encoding().GetString( input.ToArray());
            }
        }

        var deserialized = JsonConvert.DeserializeObject<List<Dog>>(readFromZipFile);
user1472131
  • 411
  • 1
  • 5
  • 15

1 Answers1

0

The following code:

    using (zipFile)
    {
        zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zipFile.UpdateEntry("dogs.txt", serialized, UTF8Encoding.UTF8);
        zipFile.Save();
    }

will dispose the zipFile when it executes. So you must create the zipFile again, before your try reading it again.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Thanks for your answer, but it is not related to the problem I am experiencing. If I use or not use the "using", the result is the same. – user1472131 Jan 25 '13 at 12:59