How to serialize a zip file into custom file format?

techwiz24

Well-known member
Joined
Jun 2, 2010
Messages
51
Programming Experience
Beginner
So, currently i'm using a binary serialization code snippet that I found. I want to know if it's possible to serialize (that's how I store the rest of the info) a zip file into the file?

Basically, my file has a few strings serialized into a file created by the program. I want to store a zip-file containing a few files, into my file-format (if you follow :D). I can post source-code if needed, but I haven't figured out the zip-file part yet. Thanks!
 
You don't serialise files. You serialise objects. The point of serialisation is to convert an arbitrarily complex object into a form that can be transmitted over a serial medium. You are already serialising an object so simply add a property to that object that can contain the data from the ZIP file. The obvious choice for that would be a Byte array. You may or may not want to also store the file name, etc.
 
In this case would it be easier to just make a zip file containing something along the lines of a "properties" file and then all the contents, and unzip it to a working directory?

Right now, i'm using this code:
VB.NET:
    Public Sub test()
        Dim settings As New Ingredient.Properties()
        Dim encoder As New Ingredient
        settings.Contents = settings.StreamFile("D:\test.zip")
        settings.IngredientVersion = "1.0"
        settings.MineCraftVersion = "Minecraft Beta 1.3_03"
        encoder.Encode("D:\", settings)
    End Sub

<Serializable()> Public Class Ingredient
        <Serializable()> Public Class Properties
            Public Function StreamFile(ByVal filename As String) As Byte()
                Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
                ' Create a byte array of file stream length
                Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
                'Read block of bytes from stream into the byte array
                fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))
                'Close the File Stream
                fs.Close()
                'return the byte data
                Return ImageData
            End Function
            '*.mpi FileType information
            Private iVersion As String              'Ingredient version
            Private impVersion As String            'MeltingPot version
            Private imcVersion As String            'Minecraft version
            Private iContents As Array    'Contents of the mod, a zipfile
            Private iContentsHash As String         'Hash of the mod contents, the zipfile
            Private iTitle As String                'The name of the mod/ingredient
            Private iCreator As String              'The creator of the mod/ingredient
            Private iOnErr As String                'The string to be displayed on an error involving the mod/ingredient
            Public Property IngredientVersion() As String
                Get
                    Return iVersion
                End Get
                Set(ByVal value As String)
                    iVersion = value
                End Set
            End Property
            Public Property ProgramVersion() As String
                Get
                    Return impVersion
                End Get
                Set(ByVal value As String)
                    impVersion = value
                End Set
            End Property
            Public Property MineCraftVersion() As String
                Get
                    Return imcVersion
                End Get
                Set(ByVal value As String)
                    imcVersion = value
                End Set
            End Property
            Public Property Contents() As Array
                Get
                    Return iContents
                End Get
                Set(ByVal value As Array)
                    iContents = value
                End Set
            End Property
            Public Property ContentsHash As String
                Get
                    Return iContentsHash
                End Get
                Set(ByVal value As String)
                    iContentsHash = value
                End Set
            End Property
            Public Property Name() As String
                Get
                    Return iTitle
                End Get
                Set(ByVal value As String)
                    iTitle = value
                End Set
            End Property
            Public Property Creator As String
                Get
                    Return iCreator
                End Get
                Set(ByVal value As String)
                    iCreator = value
                End Set
            End Property
            Public Property OnErrEvent As String
                Get
                    Return iOnErr
                End Get
                Set(ByVal value As String)
                    iOnErr = value
                End Set
            End Property
        End Class
        Public Sub Encode(ByVal strPath As String, ByVal options As Ingredient.Properties)
            ' create a filestream to allow saving the file after it has 
            ' been serialized in this method
            Dim fs As New FileStream(strPath, FileMode.OpenOrCreate)
            ' create a new instance of the binary formatter
            Dim formatter As New BinaryFormatter
            Try
                ' save the serialized data to the file path specified
                formatter.Serialize(fs, options)
                fs.Close()
            Catch ex As SerializationException
                Debugger.append(System.DateTime.Now, "CRITICAL", "FileEncode-Error! " & ex.ToString & Environment.NewLine & ex.StackTrace)
                MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Class Decode
            Public Function Deserialize(ByVal strPath As String) As Ingredient.Properties
                ' create filestream allowing the user to open an existing file
                Dim fs As New FileStream(strPath, FileMode.Open)
                ' create a new instance of the Personal Data class
                Dim strings As Ingredient.Properties
                strings = New Ingredient.Properties
                Try
                    ' create a binary formatter
                    Dim formatter As New BinaryFormatter
                    ' deserialize the data stored in the specified file and 
                    ' use that data to populate the new instance of the personal data class.
                    strings = formatter.Deserialize(fs)
                    fs.Close()
                    ' return the deserialized data back to the calling application
                    Return strings
                Catch ex As SerializationException
                    debugger.append(System.DateTime.Now, "CRITICAL", "FileEncode-Error! " & ex.ToString & Environment.NewLine & ex.StackTrace)
                    MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return strings
                End Try
            End Function
        End Class
But i'm getting System.UnauthorizedAccessException errors?
 
Back
Top