{"id":1332,"date":"2024-08-09T08:00:00","date_gmt":"2024-08-09T00:00:00","guid":{"rendered":"http:\/\/www.wyrcad.com\/?p=1332"},"modified":"2024-07-19T10:51:04","modified_gmt":"2024-07-19T02:51:04","slug":"zip-%e4%b8%8e-unzip","status":"publish","type":"post","link":"https:\/\/www.wyrcad.com\/?p=1332","title":{"rendered":"Zip \u4e0e UnZip"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Option Explicit<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8216;Example<br>Dim FileNames As Variant &#8216;Variant \u5fc5\u987b\u7684<br>Dim ZipFileName As String<br>sFilter = &#8220;All Files(<em>.<\/em>)|<em>.<\/em>&#8220;<br>&#8216;Get the file names<br>Dim NameGroup As String<br>NameGroup = <a href=\"https:\/\/www.wyrcad.com\/?p=1337\">DialogOpenFile<\/a>(Me.hwnd, &#8220;\u9009\u62e9\u6587\u4ef6&#8221;, sFilter, App.Path)<br>If NameGroup = &#8220;&#8221; Then Exit Sub<br>Dim TempArray As Variant<br>TempArray = Split(NameGroup, vbNullChar)<br>If UBound(TempArray) &#8211; LBound(TempArray) &lt; 3 Then &#8216;\u5355\u9009\u6216\u591a\u9009\u9009\u4e86\u4e00\u4e2a<br>ReDim FileNames(0 To 0)<br>FileNames(0) = TempArray(0)<br>Else &#8216;\u591a\u9009\u9009\u4e86\u591a\u4e2a<br>ReDim FileNames(0 To UBound(TempArray) &#8211; LBound(TempArray) &#8211; 3)<br>For I = 0 To UBound(TempArray) &#8211; LBound(TempArray) &#8211; 3<br>FileNames(I) = <a href=\"https:\/\/www.wyrcad.com\/?p=1239\">GetFullName<\/a>(TempArray(0), TempArray(I + 1))<br>Next I<br>End If<br>&#8216;Exit if dialog box canceled<br>If Not IsArray(FileNames) Then Exit Sub<br>If UBound(FileNames) &#8211; LBound(FileNames) &lt; 0 Then Exit Sub<br>For I = LBound(FileNames) To UBound(FileNames)<br>If Not <a href=\"https:\/\/www.wyrcad.com\/?p=1239\">FileExist<\/a>(FileNames(I)) Then Exit Sub<br>Next I<br>Dim RecommendedName As String<br>RecommendedName = <a href=\"https:\/\/www.wyrcad.com\/?p=1239\">GetShortPath<\/a>(FileNames(LBound(FileNames)))<br>sFilter = &#8220;Zip File(<em>.zip)|<\/em>.zip&#8221;<br>sTitle = &#8220;Save Zip File&#8221;<br>&#8216;\u8c03\u7528\u7a97\u4f53\u53e5\u67c4\uff0cOpen(1)\u8fd8\u662fSave(\u975e1)\uff0c\u6807\u9898\uff0c\u6253\u5f00\u65f6\u81ea\u52a8\u586b\u5165\u7684\u6587\u4ef6\u540d\uff0c\u6a21\u677f\uff0c\u6253\u5f00\u65f6\u81ea\u52a8\u9009\u62e9\u7684\u8def\u5f84\uff0c\u65e0\u6269\u5c55\u540d\u65f6\u81ea\u52a8\u589e\u52a0\u7684\u6269\u5c55\u540d\uff0c\u76ee\u6807\u8def\u5f84<br>ZipFileName = <a href=\"https:\/\/www.wyrcad.com\/?p=1337\">DialogFile<\/a>(Me.hwnd, 0, sTitle, RecommendedName, sFilter, <a href=\"https:\/\/www.wyrcad.com\/?p=1239\">GetPath<\/a>(FileNames(LBound(FileNames))), &#8220;zip&#8221;, GetPath(FileNames(LBound(FileNames))), 1)<br>ZipFileName = Trim(ZipFileName)<br>If ZipFileName = &#8220;&#8221; Then Exit Sub<br>Call Zip(FileNames, ZipFileName)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Public Sub Zip(ByVal vFileNames As Variant, ByVal vZipFileName As Variant) &#8216;Variant \u5fc5\u987b\u7684<br>On Error Resume Next &#8216;\u6709\u7684\u7cfb\u7edf\u4e0d\u652f\u6301\u538b\u7f29\u4e0e\u89e3\u538b<br>Dim I As Integer, J As Integer<br>If Not IsArray(vFileNames) Then Exit Sub<br>Dim oShell As Object<br>&#8216;Create empty Zip File with zip header<br>Dim ZipFileNumber As Integer<br>ZipFileNumber = FreeFile<br>Open vZipFileName For Output As #ZipFileNumber<br>Print #ZipFileNumber, Chr$(80) &amp; Chr$(75) &amp; Chr$(5) &amp; Chr$(6) &amp; String(18, 0)<br>Close #ZipFileNumber<br>Set oShell = CreateObject(&#8220;Shell.Application&#8221;) &#8216;\u57fa\u4e8e shell32.dll<br>&#8216;Copy the files to the compressed folder<br>Dim iCount As Integer<br>iCount = 0<br>For I = LBound(vFileNames) To UBound(vFileNames)<br>DoEvents<br>oShell.NameSpace(vZipFileName).CopyHere vFileNames(I)<br>&#8216;Debug.Print CStr(I) &amp; Space$(1) &amp; &#8220;=&#8221; &amp; Space$(1) &amp; vFileNames(I)<br>&#8216;Keep script waiting until Compressing is done<br>iCount = iCount + 1<br>On Error Resume Next<br>Do Until oShell.NameSpace(vZipFileName).items.Count = iCount<br>DoEvents<br>Wait 50<br>Loop<br>On Error GoTo 0<br>&#8216;ShowMsg frmMain, &#8220;Zip File&#8221; &amp; iCount &amp; Space$(1) &amp; &#8220;\/&#8221; &amp; Space$(1) &amp; (UBound(vFileNames) &#8211; LBound(vFileNames) + 1)<br>Next I<br>Set oShell = Nothing<br>On Error GoTo 0<br>End Sub<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Public Sub UnZip(ByVal vZipFile As Variant, ByVal vTargetFolder As Variant) &#8216;Variant \u5fc5\u987b\u7684<br>On Error Resume Next &#8216;\u6709\u7684\u7cfb\u7edf\u4e0d\u652f\u6301\u538b\u7f29\u4e0e\u89e3\u538b<br>Dim oShell As Object<br>&#8216;Copy the zipped files to the newly created folder<br>Set oShell = CreateObject(&#8220;Shell.Application&#8221;) &#8216;\u57fa\u4e8e shell32.dll<br>oShell.NameSpace(vTargetFolder).CopyHere oShell.NameSpace(vZipFile).items<br>Set oShell = Nothing<br>On Error GoTo 0<br>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Option Explicit &#8216;ExampleDim FileNames As Variant  &hellip; <a href=\"https:\/\/www.wyrcad.com\/?p=1332\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cZip \u4e0e UnZip\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[24,17],"class_list":["post-1332","post","type-post","status-publish","format-standard","hentry","category-bianchengyuandi","tag-vb","tag-biancheng"],"_links":{"self":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1332"}],"version-history":[{"count":4,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1332\/revisions"}],"predecessor-version":[{"id":1387,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=\/wp\/v2\/posts\/1332\/revisions\/1387"}],"wp:attachment":[{"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wyrcad.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}