Can Windows\' built-in ZIP compression be scripted?

Answer

There are VBA methods to zip and unzip using the windows built in compression as well, which should give some insight as to how the system operates. You may be able to build these methods into a scripting language of your choice.

The basic principle is that within windows you can treat a zip file as a directory, and copy into and out of it. So to create a new zip file, you simply make a file with the extension .zip that has the right header for an empty zip file. Then you close it, and tell windows you want to copy files into it as though it were another directory.

Unzipping is easier - just treat it as a directory.

In case the web pages are lost again, here are a few of the relevant code snippets:

ZIP

Sub NewZip(sPath)'Create empty Zip File'Changed by keepITcool Dec-12-2005If Len(Dir(sPath))>0Then Kill sPath
    Open sPath For Output As#1
    Print #1, Chr$(80)& Chr$(75)& Chr$(5)& Chr$(6)&String(18,0)
    Close #1EndSubFunction bIsBookOpen(ByRef szBookName AsString)AsBoolean' Rob BoveyOnErrorResumeNext
    bIsBookOpen =Not(Application.Workbooks(szBookName)IsNothing)EndFunctionFunction Split97(sStr AsVariant, sdelim AsString)AsVariant'Tom Ogilvy
    Split97 = Evaluate("{"""& _
                       Application.Substitute(sStr, sdelim,""",""")&"""}")EndFunctionSub Zip_File_Or_Files()Dim strDate AsString, DefPath AsString, sFName AsStringDim oApp AsObject, iCtr AsLong, I AsIntegerDim FName, vArr, FileNameZip

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

    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)=FalseThen'do nothingElse'Create empty Zip File
        NewZip (FileNameZip)Set oApp = CreateObject("Shell.Application")
        I =0For 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 doneOnErrorResumeNextDoUntil oApp.Namespace(FileNameZip).items.Count = I
                    Application.Wait (Now + TimeValue("0:00:01"))LoopOnErrorGoTo0EndIfNext iCtr

        MsgBox "You find the zipfile here: "& FileNameZip
    EndIfEndSub

UNZIP

Sub Unzip1()Dim FSO AsObjectDim oApp AsObjectDim Fname AsVariantDim FileNameFolder AsVariantDim DefPath AsStringDim strDate AsString

    Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                        MultiSelect:=False)If Fname =FalseThen'Do nothingElse'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 &"\"EndIf'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 folderSet 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

        OnErrorResumeNextSet FSO = CreateObject("scripting.filesystemobject")
        FSO.deletefolder Environ("Temp")&"\Temporary Directory*",TrueEndIfEndSub

All windows Questions

Ask your interview questions on windows

Write Your comment or Questions if you want the answers on windows from windows Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---