0

我正在尝试开发代码以从 Silverlight 5 中的 zip 文件中解压缩文件。这些文件位于 zip 文件中的目录中。

因为我们是一家 VB 商店,所以我将我在其他地方找到的这段代码从 c# 翻译成 VB。它在第四行“对象引用未设置为对象的实例。”失败。我现在意识到问题是第三行需要一个相对 uri,我正在向它传递一个文件,但我不知道如何解决这个问题。

你能告诉我这段代码有什么问题吗?我也欢迎其他想法。

谢谢。

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
4

3 回答 3

0

抱歉,我将有时间探索这些建议。我确实找到了自己实现目标的方法。请参阅下面的代码。我将使用此代码,因为这里的技术负责人更喜欢我使用 WCF 服务以不同的方式执行此操作。警告我无法 100% 测试此代码,因为我不打算使用它,但它接近正确。

导入 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
于 2013-04-03T16:30:36.913 回答
0

查看我的博文:http: //www.sharpgis.net/post/2010/08/25/REALLY-small-unzip-utility-for-Silverlight-e28093-Part-2.aspx

于 2013-03-26T14:38:35.987 回答
0

在 Silverlight 中有一种快速而肮脏的解压缩方式。使用 Application.GetResourceStream 方法。 http://msdn.microsoft.com/en-us/library/cc190632(v=vs.95).aspx

于 2013-03-26T14:25:36.643 回答