Wednesday, June 11, 2008

Zip files with the default Windows XP zip program

Interesting technique - Zip file or files with the default Windows XP zip program, never thought of it, but always used 3rd party zip utilities.

12 comments:

  1. Ciao Alex!!! :-)
    Does this technique work with VBA in Access?
    I've tried http://www.rondebruin.nl/files/windowsxpunzip.txt
    but GetOpenFileName isn't recognized as a method or data member.

    ReplyDelete
  2. Ciao Giorgio!
    yes, GetOpneFileName and browse for folder does not work in access, so you have to replace them with API calls like at www.mvps.org/access

    ReplyDelete
  3. Thank you Alex, I'm using the API described here:
    http://www.mvps.org/access/api/api0001.htm
    but I don't understand this line:
    strFilter = ahtAddFilterItem(strFilter, "Excel Files (*.XLS)", "*.XLS")
    strFilter is calling itself as an argument of the ahtAddFilterItem function?!

    ReplyDelete
  4. Anyway, I've tried and modified Sub Unzip2() at http://www.rondebruin.nl/files/windowsxpunzip.txt using the API call
    but the code stops at the line
    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(strInputFileName).Items
    with the message 'run-time error 91 "Object variable or with block variable not set'

    Here's the modified sub:
    Sub Unzip2() 'http://www.rondebruin.nl/files/windowsxpunzip.txt
    Dim FSO As Object
    Dim oApp As Object
    Dim fname
    Dim FileNameFolder
    Dim DefPath As String

    'fname = Application.GetOpenFileName(filefilter:="Zip Files (*.zip), *.zip", _
    ' MultiSelect:=False)
    Dim strFilter As String
    Dim strInputFileName As String

    strFilter = ahtAddFilterItem(strFilter, "Zip Files (*.zip), *.zip") 'http://www.mvps.org/access/api/api0001.htm
    strInputFileName = ahtCommonFileOpenSave( _
    Filter:=strFilter, OpenFile:=True, _
    DialogTitle:="Please select an input file...", _
    Flags:=ahtOFN_HIDEREADONLY)
    If strInputFileName = "" Then
    'do nothing
    Else
    DefPath = "C:\Test\" '<<< Change path
    If Right(DefPath, 1) <> "\" Then
    DefPath = DefPath & "\"
    End If

    FileNameFolder = DefPath

    Set oApp = CreateObject("Shell.Application")
    'Copy the files in the newly created folder
    oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(strInputFileName).Items

    MsgBox "You find the files here: " & FileNameFolder
    On Error Resume Next
    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.DeleteFolder Environ("Temp") & "\Temporary Directory*", True

    Set oApp = Nothing
    Set FSO = Nothing
    End If
    End Sub

    ReplyDelete
  5. Found the solution, in the sub posted previously, I use
    Dim strInputFileName
    instead of
    Dim strInputFileName as String

    ReplyDelete
  6. Ciao Giorgio!
    strFilter = ahtAddFilterItem(strFilter...
    see the function - it adds filters to a string strFilter in some format, you can call it several times to add more filters

    .CopyHere - i think you can try to use FileCopy

    Thanks for posting!

    ReplyDelete
  7. Alex, in Sub Unzip2() above, why are the lines
    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.DeleteFolder Environ("Temp") & "\Temporary Directory*", True
    used? Why do you need to delete the temporary folder?

    ReplyDelete
  8. Hi Giorgio,
    i do not see such line in originale code, but if you create a temp dir - then it is good idea to delete it after all

    ReplyDelete
  9. ???
    Of course those lines are present in the original code. Please check
    http://www.rondebruin.nl/files/windowsxpunzip.txt

    ReplyDelete
  10. ok, i looked at htm file only. no idea why he removes these temp folders, perhaps they are created automatiacally during zip process. Anyway - i would better use RmDir instead of FSO

    ReplyDelete
  11. Thank you Alex, can you use the asterisk with RmDir as well?
    RmDir(Environ("Temp") & "\Temporary Directory*")

    ReplyDelete
  12. no, but you can use dir() for get all such directories and then run rmdir for each

    ReplyDelete