7 Replies - 6728 Views - Last Post: 15 December 2011 - 08:27 AM Rate Topic: -----

#1 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

File container - "Attempting to deserialize an empty stream."

Posted 13 December 2011 - 08:33 AM

Here is the whole test project I am working on. I found this on MSDN and the issue never got resolved, but I figure it would give me an idea of where to start for creating a container to hold a set of files of varying file types. No compression is needed, so just something very simple.

Imports System.IO

Public Class Form1

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim S() As String = OpenFileDialog1.FileNames

            Dim File As String

            Dim sr As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)

            Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            For Each File In S

                lstFiles.Items.Add(File)

            Next

            sr.Close()

        End If

    End Sub

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim sw As New IO.FileStream(SaveFileDialog1.FileName, IO.FileMode.Create)

            Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            Dim file As IO.File = CType(fmt.Deserialize(sw), Object)

            For Each item As String In lstFiles.Items

                file = CType(file.Create(item), Object)

                fmt.Serialize(sw, file)

            Next

            sw.Close()

        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lstFiles.Items.Clear()

    End Sub

End Class



So, I can add files to the lstFiles, then when I click btnCreate I enter the filename I want to save it as, then I get the error: "Attempting to deserialize an empty stream."

I'm not even sure if this is the proper way to go about this and wanted to see if there was a better way, or if there is a way to fix the code above to do this.

Is This A Good Question/Topic? 0
  • +

Replies To: File container - "Attempting to deserialize an empty stream."

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: File container - "Attempting to deserialize an empty stream."

Posted 13 December 2011 - 02:29 PM

You have declared the StreamWriter - that's good...
However, you deserialise the StreamWriter before it has any data to stream - you aren't telling it what to write.
Was This Post Helpful? 1
  • +
  • -

#3 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 13 December 2011 - 04:10 PM

I am guessing you are referring to this:
Dim file As IO.File = CType(fmt.Deserialize(sw), Object)



I actually have no clue why that is like that, because it is defining what file will be, then again in the For/Each statement. So I commented it out, tried it again and the test image I was trying to add is now corrupted. Next I tried:

Dim file As IO.File = CType(fmt.Serialize(sw), Object)



...but then I get the error: Overload resolution failed because no accessible 'Serialize' accepts this number of arguments." because it requires an additional "graph as Object" for which I have no clue...

You gave me a hint

Quote

you aren't telling it what to write.

but I thought I was telling it what to write in the first line of my For/Each statement. I am going to have to read about serializing again to see if I can get any idea of how to resolve this.

Thanks for the response.
Was This Post Helpful? 0
  • +
  • -

#4 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 13 December 2011 - 04:27 PM

I edited the create sub...currently it works just using bitmaps:
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim sw As New IO.FileStream(SaveFileDialog1.FileName, IO.FileMode.Create)

            Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            Dim bmp As Object

            For Each item As String In lstFiles.Items

                bmp = CType(Bitmap.FromFile(item), Bitmap)

                fmt.Serialize(sw, bmp)

            Next

            sw.Close()

        End If

    End Sub



Nothing is coming up in the editor about adding any file type though, which is why I had initially changed things because this has to hold several different types of files.

But when I did successfully create the file, it type was "file", which I don't see where to add the extension :(.

I am beginning to think that this is the wrong way to go about creating a custom file to hold various files in it, because I saw nothing in the original example of how to read the items in it, so I will have to figure out how to do that if this is even the proper way. Is it possible that I am not looking for a "file container" but something else?
Was This Post Helpful? 0
  • +
  • -

#5 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 13 December 2011 - 09:31 PM

I was right, I was going about it the wrong way (from my noobish point of view). I didn't need anything compressed, so it turned me away from even looking at zip files. However, I never thought to just create a zip file and not compress it. I used a cross between the code I initially had except I replaced the btnCreate sub with:

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim zipPath As String = SaveFileDialog1.FileName

            Dim zip As Package = ZipPackage.Open(zipPath, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite)

            For Each item As String In lstFiles.Items

                AddToArchive(zip, item)

            Next

            zip.Close()

        End If



and the example here: http://www.codeproje...vb/ZipDemo.aspx
All I did was import System.IO.Packaging, added a reference to WindowsBase, and used the AddToArchive sub from the example and changed the compression for pkgPart to NotCompressed.

All of this spaghetti code allows me to add all the items to the lstItems, and create any filetype that I want the container to be saved as. There is a lot I have to go through and cleanup, as well as lookup on creating an actual filetype for these containers I was wanting to use in my program, but I wanted to post the solution I figured out just in case anyone else ran into this issue and needed an idea how to solve it.

However, if someone here knows of a much better way to do this, or atleast what it is called, then please feel free to offer any suggestions ;)
Was This Post Helpful? 0
  • +
  • -

#6 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 14 December 2011 - 08:43 AM

I am going to ask this here so I don't begin another thread on a somewhat similar issue. I have no clue what to look for, so I want to explain the situation and see if someone can suggest what I am supposed to look for.

Say I have created a file like above, and used it in my program. I could set the file to open with my program (which I know how to do), but what I don't get is how do I ensure that the file that is opened is for my program?

Regardless of creating a new extension to use and registering it on the computer, if another program comes along and uses the exact same 3 letter extension, there is likely to cause some issues when my program goes to open it.

On some applications that use special files and types for that program I have noticed that trying to open up another file for another program that uses the same extension, the first program will say something like "wxyz.bla is not a valid file", even though they have the same extension.

What I am looking for is what this is called, or what this deals with...I have no clue how to shorten the explanation above for google...
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,662
  • Joined: 23-August 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 15 December 2011 - 08:13 AM

Quote

Say I have created a file like above, and used it in my program. I could set the file to open with my program (which I know how to do), but what I don't get is how do I ensure that the file that is opened is for my program?


Short answer is, you can't. If someone uses the same file extension and registers their application as the default application for that sort of file, you're out of luck.
Was This Post Helpful? 1
  • +
  • -

#8 Recoil  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 292
  • Joined: 28-June 08

Re: File container - "Attempting to deserialize an empty stream."

Posted 15 December 2011 - 08:27 AM

Thanks. Aside from encrypting the file, after much deliberation I believe I could just add an additional file (XML) to say that it is used for my program...if that file doesn't exist, then return the file as unsupported.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1