使用 vba 将 gz 解压缩到 zip 文件时出错

Error when extracting gz to zip file using vba

提问人:Yunjeong Lim 提问时间:10/24/2023 更新时间:10/24/2023 访问量:49

问:

我想从这个网站下载.csv文件(https://bulk.meteostat.net/v2/hourly/41024.csv.gz) 这是数据文档(https://dev.meteostat.net/bulk/hourly.html)

这是我的代码。 我不能使用任何其他程序,如 winzip32,因为我应该将其共享给其他人。 所以我首先尝试将.gz文件转换为.zip并尝试解压缩文件。 但它根本不起作用。我的代码有问题吗?

是否有任何解决方法可以使用 VBA 解压缩 .gz 文件?我搜索了所有方法,但只有 winzip32 方法。.


Sub getResult()


    Dim station As String
    Dim startYear As String
    Dim endYear As String
    
    startYear = "2018"
    endYear = "2022"
    
    station = 41024
    makeFolder ("C:\myfile\")
    
    Call DownloadFile(startYear, endYear, station)

End Sub


Sub makeFolder(url As String)
    If Dir(url, vbDirectory) = "" Then
        MkDir (url)
    Else
    End If
End Sub

Sub DownloadFile(startYear As String, endYear As String, station As String)

    Dim year As Integer
    
    For year = CInt(startYear) To CInt(endYear)
    
        Dim myURL As String
        Dim path As String
        Dim path2 As String
        Dim file As String
        
        myURL = "https://bulk.meteostat.net/v2/hourly/" & startYear & "/" & station & ".csv.gz"
        path = "C:\myfile\" & station
        path2 = path & "\" & year
        
        
        Dim WinHttpReq As Object
        Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
        WinHttpReq.Open "GET", myURL, False, "username", "password"
        WinHttpReq.send
        
        If WinHttpReq.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            oStream.Open
            oStream.Type = 1
            oStream.Write WinHttpReq.responseBody
            makeFolder (path)
            makeFolder (path2)
            file = path2 & "\" & station & ".csv.zip"
            

            oStream.SaveToFile file, 2 ' 1 = no overwrite, 2 = overwrite
            oStream.Close
            
            Call Unzip(path2, file)
        End If

    Next year

End Sub





Private Sub Unzip(path As String, filepath As String)
Dim FSO As Object
Dim sfolder As String
Dim sApp As Object
Dim MyFile As String
Dim ExtractTo As Variant
Dim fileName As String
Dim objZipItems As FolderItems

    Set FSO = CreateObject("Scripting.FileSystemObject")
    ExtractTo = path & "\Extracted"
    If Len(Dir(ExtractTo, vbDirectory)) = 0 Then
        MkDir ExtractTo
    End If

    MyFile = Dir(filepath, vbNormal)
    Do While MyFile <> ""
        If Right(MyFile, 3) = "zip" Then
        Set sApp = CreateObject("Shell.Application")
        Set objZipItems = sApp.Namespace(path).items
       sApp.Namespace(ExtractTo).CopyHere objZipItems
        DoEvents
        End If
        MyFile = Dir
    Loop
End Sub



Excel VBA 压缩 gzip

评论

0赞 user10186832 10/24/2023
试试这个...stackoverflow.com/questions/11827578/......
0赞 Shrotter 10/24/2023
您可以尝试使用 tar 命令查看 ss64.com/nt/tar.html

答: 暂无答案