Unzipping XML Files using SharpZipLib adds extra lines to the end (only sometimes)

154 views Asked by At

This is my first attempt at a post, so I'm sorry if I leave out some important details.

The company I work for has a program that downloads a zipped file to a computer, and then unzips it into the folder the zip file is in. There are several different types of files in the zip (exe, dll, xml) and all of them unzip, however sometimes (not always) the xml file has appended lines

At the very end of the xml file there are sometimes repeated lines from earlier in the file, and it seems to run to the end of the file. Sorry that's a little hard to explain, here's the latest example:

the last section of the file was a closing element "< /root>" and immediately afterwards there was an extra "oot>"

Sometimes the amount of repeated lines is larger, but it always seems to go from somewhere before the end to the end of the file.

Here is the piece of the code that unzips the files:

    MyFileStream = New FileStream(ExtractDir & "\" & _
      MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write)
    Dim count As Integer
    Dim buffer(4096) As Byte
    count = MyZipInputStream.Read(buffer, 0, 4096)
    While count > 0
      MyFileStream.Write(buffer, 0, count)
      count = MyZipInputStream.Read(buffer, 0, 4096)
    End While
    MyFileStream.Close()
  End If

Notes: The files are zipped using just ordinary rightclick -> send to -> compressed folder

the files are unzipped using SharpZipLib

This may happen to all text files, I'm not sure and am not able to test that, only seen on xml

I'm not looking for a better way to unzip files (This isn't my code specifically and I don't want to change the way it's done unless there is no other way), I'm just looking for an explanation about how the extra lines are being added. As I mentioned above - this problem only happens some of the time, other times the xml files come out properly. The same file can be downloaded to different computers and come out differently.

I appreciate any answers, help me understand this

1

There are 1 answers

1
the_lotus On BEST ANSWER

When you unzip the xml, make sure you create a new empty file. If you modify a file that already exists you will get this problem if the new file is smaller by using OpenOrCreate, you'll end up with some residu of the old file.

An other option is to delete the files. You can delete them before creating (one by one) or delete the whole folder before calling your program.