0

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

AaronTheRabbit
  • 185
  • 1
  • 8
  • When you unzip the xml, do you create a new empty file or modify the one that is currently there? With OpenOrCreate, you might have some residu of the old file if the new file is smaller. – the_lotus Nov 12 '14 at 19:47
  • The file usually already exists, I'm pretty sure your answer explains exactly what is happening to us. It will be a while before I can actually test this - I have to get permission to change the code - but we want to completely overwrite the old file with the new one, so we should change FileMode.OpenOrCreate to FileMode.Create, correct? – AaronTheRabbit Nov 12 '14 at 20:33
  • 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. – the_lotus Nov 13 '14 at 11:19
  • I've been able to test this change now and your first comment was exactly the right answer, is there any way to give you credit for this? My understanding is that comments can't be marked as answers, maybe you could repost your comment as the answer so I can mark it correct? – AaronTheRabbit Nov 13 '14 at 13:36
  • I added an answer, you can mark it as the right answer. Good to see that it worked! – the_lotus Nov 13 '14 at 13:41

1 Answers1

0

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.

the_lotus
  • 12,668
  • 3
  • 36
  • 53