2008-10-07 45 views
6

Có cách nào để tạo một thư mục nén trong Windows theo chương trình không? Tôi không thể nhìn thấy một cách để làm điều này bằng cách sử dụng FileSystemObject (mặc dù có thuộc tính 'Nén').Tạo thư mục nén (hoặc nén)

Tôi đã xem tệp nén của dll nhưng tôi muốn tránh phải phân phối lại một dll nếu có thể. Windows XP tự nhiên hỗ trợ các thư mục nén.

+0

Câu hỏi trùng lặp, xem [Windows built-in nén ZIP kịch bản-thể?] (Http://stackoverflow.com/questions/30211/windows-built-in-zip-compression-script-able#124775) Tôi cũng đã trả lời câu hỏi ở đó với một số mã mẫu và một vài liên kết: Jay

+0

Xem câu hỏi sau : [http://stackoverflow.com/questions/118547/creating-a-zip-file-on-windows-xp2003-in-cc](http://stackoverflow.com/questions/118547/creating-a-zip -file-on-windows-xp2003-in-cc). – warren

Trả lời

6

Có một cái nhìn tại các liên kết sau đây:

http://www.rondebruin.nl/windowsxpzip.htm

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1383147&SiteID=1

Tước những phần quan trọng từ ví dụ first link có thể chứng minh là đủ.

Sub NewZip(sPath) 
'Create empty Zip File 
'Changed by keepITcool Dec-12-2005 
    If Len(Dir(sPath)) > 0 Then Kill sPath 
    Open sPath For Output As #1 
    Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0) 
    Close #1 
End Sub 

Function Split97(sStr As Variant, sdelim As String) As Variant 
'Tom Ogilvy 
    Split97 = Evaluate("{""" & _ 
         Application.Substitute(sStr, sdelim, """,""") & """}") 
End Function 

Sub Zip_File_Or_Files() 
    Dim strDate As String, DefPath As String, sFName As String 
    Dim oApp As Object, iCtr As Long, I As Integer 
    Dim FName, vArr, FileNameZip 

    DefPath = Application.DefaultFilePath 
    If Right(DefPath, 1) <> "\" Then 
     DefPath = DefPath & "\" 
    End If 

    strDate = Format(Now, " dd-mmm-yy h-mm-ss") 
    FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip" 

    'Browse to the file(s), use the Ctrl key to select more files 
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _ 
        MultiSelect:=True, Title:="Select the files you want to zip") 
    If IsArray(FName) = False Then 
     'do nothing 
    Else 
     'Create empty Zip File 
     NewZip (FileNameZip) 
     Set oApp = CreateObject("Shell.Application") 
     I = 0 
     For iCtr = LBound(FName) To UBound(FName) 
      vArr = Split97(FName(iCtr), "\") 
      sFName = vArr(UBound(vArr)) 
      If bIsBookOpen(sFName) Then 
       MsgBox "You can't zip a file that is open!" & vbLf & _ 
         "Please close it and try again: " & FName(iCtr) 
      Else 
       'Copy the file to the compressed folder 
       I = I + 1 
       oApp.Namespace(FileNameZip).CopyHere FName(iCtr) 

       'Keep script waiting until Compressing is done 
       On Error Resume Next 
       Do Until oApp.Namespace(FileNameZip).items.Count = I 
        Application.Wait (Now + TimeValue("0:00:01")) 
       Loop 
       On Error GoTo 0 
      End If 
     Next iCtr 

     MsgBox "You find the zipfile here: " & FileNameZip 
    End If 
End Sub 
+0

tôi tin rằng điều này không thành công nếu các mục nằm trong các thư mục. Nếu thư mục nguồn chứa 20 mục, thì không gian tên của nó sẽ báo cáo 20, nhưng không gian tên zip sẽ vẫn báo cáo chỉ 1 mục-- thư mục. –

Các vấn đề liên quan