Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Net如何将压缩文本文件直接读入字符串数组_Vb.net_File Io_Zipfile - Fatal编程技术网

Vb.net Net如何将压缩文本文件直接读入字符串数组

Vb.net Net如何将压缩文本文件直接读入字符串数组,vb.net,file-io,zipfile,Vb.net,File Io,Zipfile,是否可以像普通文本文件一样读取压缩后的文本文件,而不必先解压缩,如果可以,如何读取?目前,我必须提取文件,将所有行读取为字符串数组,然后删除提取的文件 Dim arrLines() As String Try oApp.Namespace(Application.StartupPath).CopyHere(oApp.Namespace(strZipFilename).Items.Item(CStr(filename))) arrLines = IO.File.ReadAllLin

是否可以像普通文本文件一样读取压缩后的文本文件,而不必先解压缩,如果可以,如何读取?目前,我必须提取文件,将所有行读取为字符串数组,然后删除提取的文件

Dim arrLines() As String
Try
    oApp.Namespace(Application.StartupPath).CopyHere(oApp.Namespace(strZipFilename).Items.Item(CStr(filename)))
    arrLines = IO.File.ReadAllLines(Application.StartupPath & "\" & filename)
    Dim FSO = CreateObject("Scripting.FileSystemObject")
    FSO.DeleteFile(Application.StartupPath & "\" & filename)
Catch ex As Exception
    Return False
End Try
'do stuff with arrLines...

我宁愿不需要这样做。如果有人能告诉我如何在不使用第三方库的情况下执行类似于
IO.File.ReadAllLines(oApp.Namespace(strZipFilename.Items.Items)(CStr(filename))
的操作,我将非常感激。

此代码用于读取GZip压缩流并一次返回压缩文件的文本。如果没有第三方库,当前无法解压缩使用PKZip压缩的文件。打包将解压一些文件,但相对有限。不过,对于内置的应用程序,也有一些限制

    Public Shared Iterator Function ReadAllCompressedLines(path As String) As IEnumerable(Of String)
        Using rawStream = New System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
            Using cstream = New System.IO.Compression.GZipStream(rawStream, System.IO.Compression.CompressionMode.Decompress)
                Dim currLine As String = ""
                Dim currentByte As Integer
                currentByte = cstream.ReadByte()
                While currentByte <> -1
                    currLine += Convert.ToChar(currentByte)
                    If currLine.EndsWith(vbCrLf) Then
                        Dim rc = Trim(currLine)
                        If rc.Length > 0 Then
                            Yield rc
                        End If
                        currLine = ""
                    End If
                    currentByte = cstream.ReadByte()
                End While
            End Using
        End Using
    End Function

    Private Shared Function Trim(currLine As String) As String
        '\x00EF\x00BB\x00BF
        Return currLine.TrimEnd(ControlChars.Cr, ControlChars.Lf).TrimStart(Convert.ToChar(&HEF), Convert.ToChar(&HBB), Convert.ToChar(&HBF))
    End Function
公共共享迭代器函数ReadAllCompressedLines(路径为字符串)作为IEnumerable(字符串的)
使用rawStream=New System.IO.FileStream(路径,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read)
使用cstream=New System.IO.Compression.gzip流(rawStream、System.IO.Compression.CompressionMode.decompresse)
Dim currLine As String=“”
将currentByte设置为整数
currentByte=cstream.ReadByte()
而当前字节为-1
currLine+=Convert.ToChar(currentByte)
如果currLine.EndsWith(vbCrLf),则
尺寸rc=装饰件(currLine)
如果rc.Length>0,则
屈服钢筋混凝土
如果结束
currLine=“”
如果结束
currentByte=cstream.ReadByte()
结束时
终端使用
终端使用
端函数
私有共享函数修剪(currLine作为字符串)作为字符串
'\x00EF\x00BB\x00BF
返回currLine.TrimEnd(ControlChars.Cr、ControlChars.Lf)、TrimStart(Convert.ToChar(&HEF)、Convert.ToChar(&HBB)、Convert.ToChar(&HBF))
端函数

您可以通过以下方式实现:

  • 添加System.IO.Compression和System.IO.Compression.FileSystem的引用
  • 添加System.IO.Compression的导入
  • 然后,您可以使用以下代码将内容读取到文件中,并将其存储到字符串变量中。在这个特定的示例中,我将压缩文件名“File1.txt”存储在主窗体(tbFile)的文本框中,并将压缩文件的内容显示在主窗体的文本框(tb1)中

    Private Sub bRead_Click(sender As Object, e As EventArgs) Handles bRead.Click
        Try
            Using archive As ZipArchive = Compression.ZipFile.Open(zipDest, ZipArchiveMode.Read)
                Dim entry As ZipArchiveEntry = archive.GetEntry(tbFile.Text)
                Dim s As String = ""
                Using sr As StreamReader = New StreamReader(entry.Open())
                    s = sr.ReadToEnd()
                End Using
                tb1.Text = s
            End Using
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    

    +1撤销意味着匿名否决票。我不知道如何在vb.net中回答这个问题,但这是一个完全合理的问题。顺便说一句,这是绝对可能的,你可以在内存中解压缩一个zip文件,尽管我不知道vb.net是否有标准的库来做这件事。谢谢。确认它是可能的意味着答案可能在某处,尽管我还没有找到。这太棒了。我是如此努力地一步完成这一切,以至于我忽略了一次读一行。谢谢!我现在不能测试,但这看起来正是我当时想要的没有问题。技术的变化和对老问题的更好的回答随之而来。我不知道这在当时是否可行,但是对于现在有同样问题的人来说,这个答案发布出来是件好事。查看这些软件包的文档,这正是我问这个问题时的想法。