2008-08-27 114 views
53

內置於Windows XP/Vista/2003/2008中的ZIP壓縮是否可以根據腳本進行編寫?我需要從BAT/CMD文件調用什麼可執行文件?或者是否可以用VBScript來做到這一點?Windows的內置ZIP壓縮可以編寫腳本嗎?

我意識到這是可能的使用WinZip,7-Zip和其他外部應用程序,但我正在尋找的東西,不需要安裝外部應用程序。

+0

在Google上有各種可用的VBScript實現,例如「[使用Windows Shell(XP,Vista,2003和2008)和VBScript壓縮和解壓縮文件](http://www.naterice.com/blog/template_permalink.asp?id=64)」。我沒有測試過這些,但它們很可能只是'拉鍊',而不是壓縮。 – samjudson 2008-08-27 14:11:11

+1

你在拉鍊和壓縮之間有什麼區別? – Cheeso 2009-07-30 04:36:53

+1

您可以將文件'壓縮'成一個文件而不壓縮它們,就像unix中的'tar'一樣。這使您可以將文件作爲包分發,但不會減小它們的磁盤空間大小。 – samjudson 2009-08-06 15:22:26

回答

28

對於zipunzip,還有使用內置壓縮的窗口的VBA方法,它應該提供關於系統如何操作的一些信息。您可以將這些方法構建爲您選擇的腳本語言。

基本原理是,在Windows中,您可以將zip文件視爲目錄,並將其拷入和拷出。因此,要創建一個新的zip文件,只需製作一個擴展名爲.zip的文件,該文件具有空白zip文件的正確標題。然後你關閉它,並告訴你想將文件複製到它的窗口,就好像它是另一個目錄一樣。

解壓縮更簡單 - 只需將其視爲目錄即可。

如果網頁被再度迷失,這裏有一些相關的代碼片段:

ZIP

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 bIsBookOpen(ByRef szBookName As String) As Boolean 
' Rob Bovey 
    On Error Resume Next 
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing) 
End Function 


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 

UNZIP

Sub Unzip1() 
    Dim FSO As Object 
    Dim oApp As Object 
    Dim Fname As Variant 
    Dim FileNameFolder As Variant 
    Dim DefPath As String 
    Dim strDate As String 

    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _ 
             MultiSelect:=False) 
    If Fname = False Then 
     'Do nothing 
    Else 
     'Root folder for the new folder. 
     'You can also use DefPath = "C:\Users\Ron\test\" 
     DefPath = Application.DefaultFilePath 
     If Right(DefPath, 1) <> "\" Then 
      DefPath = DefPath & "\" 
     End If 

     'Create the folder name 
     strDate = Format(Now, " dd-mm-yy h-mm-ss") 
     FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\" 

     'Make the normal folder in DefPath 
     MkDir FileNameFolder 

     'Extract the files into the newly created folder 
     Set oApp = CreateObject("Shell.Application") 

     oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items 

     'If you want to extract only one file you can use this: 
     'oApp.Namespace(FileNameFolder).CopyHere _ 
     'oApp.Namespace(Fname).items.Item("test.txt") 

     MsgBox "You find the files here: " & FileNameFolder 

     On Error Resume Next 
     Set FSO = CreateObject("scripting.filesystemobject") 
     FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True 
    End If 
End Sub 
1

SourceForge上提供的UnxUtils軟件包(http://sourceforge.net/projects/unxutils)上有zip和unzip可執行文件(以及其他有用應用程序的船載)。將它們複製到PATH中的某個位置,例如'c:\ windows',並且可以將它們包含在腳本中。

這不是一個完美的解決方案(或者你要求的解決方案),而是一個體面的工作。

+2

UnxUtils項目似乎不再被維護(自2007年以來沒有任何新增功能)。 [GNUWin32項目](http://gnuwin32.sourceforge.net/)在Windows上具有更新版本的GNU工具。它包括[InfoZip](http://www.info-zip.org/)的zip/unzip壓縮版本。但是直接InfoZip [從他們自己的源代碼]構建(http://sourceforge.net/projects/infozip/files/)會更好更安全。 – dolmen 2011-03-26 23:00:46

24

是的,這可以用腳本VBScript中。例如下面的代碼可以從目錄創建一個ZIP:

Dim fso, winShell, MyTarget, MySource, file 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set winShell = createObject("shell.application") 


MyTarget = Wscript.Arguments.Item(0) 
MySource = Wscript.Arguments.Item(1) 

Wscript.Echo "Adding " & MySource & " to " & MyTarget 

'create a new clean zip archive 
Set file = fso.CreateTextFile(MyTarget, True) 
file.write("PK" & chr(5) & chr(6) & string(18,chr(0))) 
file.close 

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items 

do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count 
    wscript.sleep 1000 
loop 

Set winShell = Nothing 
Set fso = Nothing 

您也可以找到http://www.naterice.com/blog/template_permalink.asp?id=64有用,因爲它包括在VBScript中一個完整的解壓縮/ ZIP實現。

如果您每500毫秒執行一次大小檢查而不是項目計數,則它對大文件更好。

set fso=createobject("scripting.filesystemobject") 
Set h=fso.getFile(DestZip) 
do 
    wscript.sleep 500 
    max = h.size 
loop while h.size > max 

的偉大工程大量的日誌文件:Win 7的,儘管它尚未完成壓縮瞬間寫入文件。

5

僅供參考:Gzip不是Guy Starbuck在8月份的評論中提出的僅MS的算法。 System.IO.Compression中的GZipStream使用Deflate算法,就像zlib庫以及其他許多zip工具一樣。該類與gzip等unix實用程序完全可互操作。

GZipStream類不能從命令行或VBScript編寫腳本來生成ZIP文件,所以它本身不會回答原始發佈者的請求。

免費的DotNetZip庫不會讀取和生成壓縮文件,並可以從VBScript或Powershell中編寫腳本。它還包括用於生成和讀取/提取壓縮文件的命令行工具。

下面是一些的VBScript代碼:

dim filename 
filename = "C:\temp\ZipFile-created-from-VBScript.zip" 

WScript.echo("Instantiating a ZipFile object...") 
dim zip 
set zip = CreateObject("Ionic.Zip.ZipFile") 

WScript.echo("using AES256 encryption...") 
zip.Encryption = 3 

WScript.echo("setting the password...") 
zip.Password = "Very.Secret.Password!" 

WScript.echo("adding a selection of files...") 
zip.AddSelectedFiles("*.js") 
zip.AddSelectedFiles("*.vbs") 

WScript.echo("setting the save name...") 
zip.Name = filename 

WScript.echo("Saving...") 
zip.Save() 

WScript.echo("Disposing...") 
zip.Dispose() 

WScript.echo("Done.") 

下面是PowerShell的一些代碼:

[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll"); 

$directoryToZip = "c:\\temp"; 
$zipfile = new-object Ionic.Zip.ZipFile; 
$e= $zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.") 
$e= $zipfile.AddDirectory($directoryToZip, "home") 
$zipfile.Save("ZipFiles.ps1.out.zip"); 

在.bat或.cmd文件,可以使用zipit.exe或包含unzip.exe工具。例如:

zipit NewZip.zip -s "This is string content for an entry" Readme.txt src 
1

創建壓縮存檔,您可以使用該實用程序MAKECAB.EXE

2

Here'a我試圖總結爲壓縮和解壓縮內置功能窗口 - How can I compress (/ zip) and uncompress (/ unzip) files and folders with batch file without using any external tools?

用幾乎沒有幾個解決方案可以在幾乎所有的Windows機器上運行

至於到shell.application和WSH我優選jscript ,因爲它允許混合料/ JScript文件(具有擴展名.BAT),其不需要臨時files.I've把解壓和zip能力在一個文件加上幾個更多的功能。

相關問題