Announcement

Collapse
No announcement yet.

Zip/Unzip Files

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gary Beene
    replied
    Interestingly, it failed in PBWin9 also - but pointed to the ENUM in the winshell.inc include file as the problem.

    That took me back a moment, until I realized that the PBWin9 and PBWin10 COM Browsers generate the include equates differently.

    The PBWin9 version does this for equates:
    Code:
    ' OfflineFolderStatus enumeration
    %OfflineFolderStatus_OFS_INACTIVE = -1
    %OfflineFolderStatus_OFS_ONLINE = 0
    %OfflineFolderStatus_OFS_OFFLINE = 1
    %OfflineFolderStatus_OFS_SERVERBACK = 2
    %OfflineFolderStatus_OFS_DIRTYCACHE = 3
    Whereas PBWin10 version does this:
    Code:
    Enum OfflineFolderStatus
        OFS_INACTIVE = -1
        OFS_ONLINE = 0
        OFS_OFFLINE = 1
        OFS_SERVERBACK = 2
        OFS_DIRTYCACHE = 3
    End Enum
    So an include generated by the PBWin10 COM browser won't run in PBWin9 because ENUM was not available in PBWin9.

    Leave a comment:


  • Gary Beene
    replied
    Hey William,
    First of all, of course, thanks for the code!

    Then, I noticed that you used this:
    Code:
    sFile = ucode$(PATHNAME$(NAME, sFrom) & PATHNAME$(EXTN, sFrom))
    Just wondering, why you didn't use the NAMEX key word?

    But most importantly, the code fails in PBWin10. I had to remove the UCode$ and do this:
    Code:
    sFile = PathName$(Namex, sFrom)
    Then it seems to work just fine.

    It worked for you as is, in PBWIn10?

    Leave a comment:


  • Gary Beene
    replied
    Hi William
    No, I haven't tried it that way. Thanks for the clarification and I'll go give your code above a try out!

    Leave a comment:


  • William Burns
    replied
    Gary, have you tried changing the code to copy a file instead of a whole folder? It is using the shell interface which is basically copying files to the zip folder so it will work with a single file or a whole folder. I use the following code which is similar to the code I posted and it can zip a single file.


    Code:
    '==================================================
    '  CreateZipFile - creates a zip file
    '==================================================
    Function CreateZipFile(byval sFrom as string, byval sTo as string) as long
       local hFile          as dword
       'Object Variables
       DIM oShellClass      AS IShellDispatch
       DIM oSourceFolder    AS Folder
       DIM oTargetFolder    AS Folder
       DIM oItem            AS FolderItem
       'variants
       DIM vSourceFolder    AS VARIANT
       DIM vTargetFolder    AS VARIANT
       DIM vOptions         AS VARIANT
       DIM vFile            AS VARIANT
       dim sFile            as string
    
       'First we create a empty ZIP file using a standard zip file header
       try
          hFile = freefile
          open sTo for output as #hFile
          print #hFile, Chr$(80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
          close #hFile
       catch
          ? "Error creating Zip file: " & sTo & "  Error:" & Error$(err)
          exit function
       end try
    
    
       ' Get an instance of our Windows Shell
       oShellClass = ANYCOM $PROGID_SHELL32_SHELL
    
       ' Did we get the object? If not, terminate this app
       IF ISFALSE ISOBJECT(oShellClass) OR ERR THEN
          ? "Could not get the Windows Shell object.  Error:" & str$(err)
          EXIT FUNCTION
       END IF
    
    
       'assign the source folder we want to zip up
       vSourceFolder = rtrim$(PATHNAME$(PATH, sFrom),"\")
       oSourceFolder = oShellClass.NameSpace(vSourceFolder)
    
       IF ISFALSE ISOBJECT(oSourceFolder) OR ERR THEN
          ? "Could not get the Source folder object.  Error:" & str$(err)
          goto TerminateZip
       END IF
    
    
       'assign the target folder we want to create (in this case it is a zip file)
       vTargetFolder = sTo
       oTargetFolder = oShellClass.NameSpace(vTargetFolder)
    
       IF ISFALSE ISOBJECT(oTargetFolder) OR ERR THEN
          ? "Could not get the Target folder object.  " & sTo & " Error:" & str$(err)
          goto TerminateZip
       END IF
    
    
       'get the file name we are copying
       sFile = ucode$(PATHNAME$(NAME, sFrom) & PATHNAME$(EXTN, sFrom))
    
       'assign the file item
       oItem = oSourceFolder.ParseName(sFile)
    
       IF ISFALSE ISOBJECT(oItem) THEN
          ? "Could not get the Item object. " & sFile & " Error:" & str$(err)
          goto TerminateZip
       END IF
    
       'now we start the copy in to the new zip file
       vOptions = 20
       oTargetFolder.CopyHere(oItem, vOptions)
    
       IF ERR THEN
          ? "Got an Error during the CopyHere method.  Error:" & str$(err)
          goto TerminateZip
       END IF
    
       'NOTE:  the above copyhere method starts a seperate thread to do the copy
       'so the command could return before the copy is finished, so we need to
       'allow time to complete.   Thus the next Sleep command.
       sleep 6000   'increase for larger folders
    
       '? sTo + " was successfully created."
       function = %TRUE
    
       TerminateZip:
    
       ' Close all of the Interfaces
    	vFile					= EMPTY
       vSourceFolder     = EMPTY
       vTargetFolder     = EMPTY
       vOptions          = EMPTY
       oItem             = NOTHING
       oTargetFolder     = NOTHING
       oSourceFolder     = NOTHING
       oShellClass       = NOTHING
    end function
    I use it like this:
    Code:
    If CreateZipFile("C:\Path\MyData.mdb", "C:\Path\MyData.zip") Then
    Last edited by William Burns; 9 May 2012, 10:21 PM.

    Leave a comment:


  • Scott Slater
    replied
    ZLib isn't that difficult to use. I know that's not what you asked for, but you should take a look at it.

    Leave a comment:


  • Michael Mattias
    replied
    The folder version is useful, but I'd also like to be able to provide a list of files and zip only those,
    If you can generate a list of 'files to be zipped', you can always create a temp directory, store your files there and zip 'em. When done zipping, delete the files and the temp directory.

    MCM

    Leave a comment:


  • José Roca
    replied
    For a single file, pass the name of the file in CopyHere.
    See: http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    Last edited by José Roca; 23 Sep 2012, 11:24 AM.

    Leave a comment:


  • José Roca
    replied
    For PB 10 you have to re-generate the interface declarations, since the old one uses STRING instead of WSTRING.

    Leave a comment:


  • Gary Beene
    replied
    And, looking at the code a bit more, I see that it was for zipping an entire folder, not a single file or a list of files. The folder version is useful, but I'd also like to be able to provide a list of files and zip only those, then unzip those files without having to create a containing folder.

    More to learn ... always easier when you have something working to start with.

    Leave a comment:


  • Gary Beene
    replied
    Hi Jose!

    You're so funny!

    Yes, I went off and tried the code at the link, and successfully created a zip file. Way cool!

    It ran in PBWin10, which I was a bit surprised at ... but I haven't used enough object code yet to know if there's some update/simplification possible since the code at the link was tested in PBWin9.

    The discussion thread suggested some timing issues also.

    But a really nice start.

    Leave a comment:


  • José Roca
    replied
    [...] but found only a few older references to using SHELL API to do the trick.
    Must be because that is what Windows uses.

    Leave a comment:


  • Gary Beene
    started a topic Zip/Unzip Files

    Zip/Unzip Files

    I went looking for some code that uses the XP/Win7 built-in ZIP API, but found only a few older references to using SHELL API to do the trick.

    Anyone have more recent experience on how to zip/unzip files within PowerBASIC?

    Or, did I perhaps simply not find the right thread?

    This was the older thread I found.
    Last edited by Gary Beene; 9 May 2012, 03:04 PM.
Working...
X
😀
🥰
🤢
😎
😡
👍
👎