Unzip a zip file in silverlight

非 Y 不嫁゛ 提交于 2020-01-17 14:02:10

问题


I am trying to develop code to unzip file from the zip file in Silverlight 5. The files are in a directory within the zip file.

I translated this code I found elsewhere from c# to VB since we are a VB shop. It is failing on the fourth line "Object reference not set to an instance of an object.". I realize now that the problem is that the third line is expecting a relative uri and I am passing it a file, but I don't know how to fix this.

Can you tell me what is wrong with this code. I will also welcome other ideas.

Thanks.

Public Shared Function GetZipContents(ByVal filename As String) As String()

        Try



            Dim zipStream As System.IO.Stream = New System.IO.MemoryStream()
            Dim zipInfo As New StreamResourceInfo(zipStream, Nothing)
            Dim streamInfo As StreamResourceInfo = Application.GetResourceStream(zipInfo, New Uri(filename, UriKind.Relative))
            Dim fileStream As Stream = streamInfo.Stream

        Dim names As New List(Of String)()
        Dim reader As New BinaryReader(fileStream)
        Do While reader.ReadUInt32() = &H4034B50

            ' Skip the portions of the header we don't care about
            reader.BaseStream.Seek(14, SeekOrigin.Current)
            Dim compressedSize As UInteger = reader.ReadUInt32()
            Dim uncompressedSize As UInteger = reader.ReadUInt32()
            Dim nameLength As Integer = reader.ReadUInt16()
            Dim extraLength As Integer = reader.ReadUInt16()
            Dim nameBytes() As Byte = reader.ReadBytes(nameLength)
            names.Add(Encoding.UTF8.GetString(nameBytes, 0, nameLength))
            reader.BaseStream.Seek(extraLength + compressedSize, SeekOrigin.Current)

        Loop
        ' Move the stream back to the begining
        fileStream.Seek(0, SeekOrigin.Begin)
        Return names.ToArray()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return Nothing
        End Try
    End Function

回答1:


There is quick and dirty way to unzip in Silverlight. Use Application.GetResourceStream Method. http://msdn.microsoft.com/en-us/library/cc190632(v=vs.95).aspx




回答2:


Check out my blogpost on this: http://www.sharpgis.net/post/2010/08/25/REALLY-small-unzip-utility-for-Silverlight-e28093-Part-2.aspx




回答3:


Sorry I would have time to explore the suggestions. I did find a way to accomplish my goal on my own. See the code below. I would be using this code either because the Technical Leader here prefers I do it a different way using a WCF service. Warning I was not able to test this code 100% since I am not planning to use it, but it is close to being right.

Imports ICSharpCode.SharpZipLib.Zip

Public Shared Sub UnZip(ByVal SrcFile As String, ByVal DstFile As String, ByVal BufferSize As Integer)

    Try
        Dim _FileName As String
        Dim _ZipEntry As ZipEntry
        Dim _FileStreamOut As FileStream = Nothing
        Dim _Done As Boolean = False


        Dim _FileStreamIn As New FileStream(SrcFile, FileMode.Open, FileAccess.Read)
        Dim _ZipInStream As New ZipInputStream(_FileStreamIn)


        Do Until _Done = True
            _ZipEntry = _ZipInStream.GetNextEntry()
            If IsNothing(_ZipEntry) Then
                _Done = True
                Exit Do
            End If
            _FileName = DstFile & "\" & _ZipEntry.Name
            _FileName = _FileName.Replace("/", "\")

            If Right(_FileName, 1) = "\" Then
                If Directory.Exists(_FileName) = False Then
                    Directory.CreateDirectory(_FileName)
                End If
            Else
                _FileStreamOut = New FileStream(_FileName, FileMode.Create, FileAccess.Write)

                Dim size As Integer
                Dim buffer(BufferSize - 1) As Byte
                Do
                    size = _ZipInStream.Read(buffer, 0, buffer.Length)
                    _FileStreamOut.Write(buffer, 0, size)
                Loop While size > 0
            End If
        Loop

        _ZipInStream.Close()
        _FileStreamOut.Close()
        _FileStreamIn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)

    End Try


End Sub


来源:https://stackoverflow.com/questions/15639458/unzip-a-zip-file-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!