Announcement

Collapse
No announcement yet.

Example of creating ZIP file using Windows Shell posted in Source Code forum

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

  • Example of creating ZIP file using Windows Shell posted in Source Code forum

    I just posted an example of how to use the new PB9 COM to interface with Windows Shell to create a new ZIP file and copy a folder and all its contents to the ZIP file. (Using the built-in zip functions of Windows XP and above) It is in the source code forum here.


    Post comments or questions here.


    William
    Last edited by William Burns; 4 Sep 2008, 09:09 AM. Reason: updated link
    "I haven't lost my mind... its backed up on tape... I think??" :D

  • #2
    William.

    The link above seems to have a problem , doubled up http's. maybe like this ...
    here
    Rick Angell

    Comment


    • #3
      Right you are. Looks like the "Insert Link" icon puts "http://" in before you paste the link and I must have pasted my full link after that.

      Thanks for the info. It's fixed now.
      "I haven't lost my mind... its backed up on tape... I think??" :D

      Comment


      • #4
        Excellent job of commenting!

        One thought....
        Code:
         '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 2000	'increase for larger folders
        I wonder if perhaps one of the...
        Code:
        Method Synchronize <1610809346> ()
        .... can be used to eliminate the 'guess' as to when the copy is finished?

        Or maybe, one of the properties you can get is either a threadid or thread handle, allowiing the use of WaitForSingleObject() ???

        Or also maybe... you could set up a FindFirstChangeNotification object on the target file, and when the copy is complete and the directory updated the object will become signalled and you could stop waiting then?



        MCM
        Last edited by Michael Mattias; 4 Sep 2008, 09:49 AM.
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Thanks. I try to comment the code well that I post since the audience normally has varied experience levels.
          ... use of WaitForSingleObject()
          Yeah I did find some discussion about using the WaitForObject calls on the new thread handle. But it looks like the best they could come up with was enumerating the threads before and after the CopyHere call to find the new thread. (that came from Microsoft's technet forum from one of the MSCEs, so I trust they know) And I wanted to keep this example simple and to the point. Adding the code needed to do the thread watching was cluttering things up. But I am still playing with it and if I find an elegant way I will post a second example that shows how to properly wait.

          William
          "I haven't lost my mind... its backed up on tape... I think??" :D

          Comment


          • #6
            I have also seen a lot of people address this issue by putting the program in a waiting loop that looks for the new zip file to see if it exists before proceeding. But I wonder if the zip file would show up before it finished with the zipping. I know when I manually create a zip file, I can see the zip file before it stops when I zip a large folder. So I am doubtful if that would work %100 of the time.

            William
            "I haven't lost my mind... its backed up on tape... I think??" :D

            Comment


            • #7
              ZipFolders can be installed to Pre-XP OS's too. I have the files and a BAT file to install it if needed. Back when I hated XP I had 2000 configured to do everything XP does except themes. Even App Compat works with a registry tweak.
              sigpic
              Mobile Solutions
              Sys Analyst and Development

              Comment


              • #8
                Originally posted by William Burns View Post
                I have also seen a lot of people address this issue by putting the program in a waiting loop that looks for the new zip file to see if it exists before proceeding. But I wonder if the zip file would show up before it finished with the zipping. I know when I manually create a zip file, I can see the zip file before it stops when I zip a large folder. So I am doubtful if that would work %100 of the time.

                William
                William, a thought occurs to me. Would it be possible to check the time stamp of the file? I dunno if it keeps track of the seconds as well but if it did, that might be a way to check.

                Another thought, when the program is finished with the zipping, could may try to FileCopy to a dummy. If getting an Error back that would indicate the zip file hasn't been completely written yet. (Windows wouldn't allow a, Open file to be copied that was still being written too. Would it?)

                FWIW.

                ==============================================
                "All truth passes through three stages.
                First, it is ridiculed.
                Second, it is violently opposed.
                Third, it is accepted as being self-evident."
                Arthur Schopenhauer (1788-1860)
                ==============================================
                It's a pretty day. I hope you enjoy it.

                Gösta

                JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                Comment


                • #9
                  >But I wonder if the zip file would show up before it finished with the zipping

                  That's what I was wondering myself: if the Copyhere method closes the target file after each file in the list has been added, which forces a directory update, which causes the FindFirstChangeNotification() object to become signalled.

                  If it's doing that, then instead of handling multiple files thru one call to CopyHere, you could add the files one at a time, and only monitor FFCN() following the last call.

                  That would also let you do a nice 'progress bar' for the user, updating after each file.

                  MCM
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Not to add more confusion, but I must add this caveat.
                    Lets say the zip does not appear until its finished writing, this could not be true if the destination is on a USB drive.

                    Although normally faster than what you would see, but files appear on the drive, but are not written until Windows has processing time free to physically write it to the disk. (This is partly why if you go to eject, you may get the message that it can not safely eject yet)

                    Just some more fodder for thought
                    Engineer's Motto: If it aint broke take it apart and fix it

                    "If at 1st you don't succeed... call it version 1.0"

                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                    Comment


                    • #11
                      Unzip ?

                      Has anybody written the COM routine to Unzip a ZIP File? I wish I had some COM understanding to do it by I don't.

                      Comment


                      • #12
                        I haven't tested it, but it should be as simple as using the CopyHere method and swapping the target and source in the example code. (skip the first part of the example code that creates the zip file)
                        "I haven't lost my mind... its backed up on tape... I think??" :D

                        Comment


                        • #13
                          Hello,
                          '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 2000 'increase for larger folders
                          That can deal one easily and surely with the following code:

                          Code:
                          ...
                          LOCAL lNr, lInFiles, lOutFiles AS LONG
                          
                          ...
                          ...
                             lInFiles = oSourceFolder.Items().COUNT
                             IF ERR THEN
                                MSGBOX "error during the Count property in SourceFolder",,"Error:" & STR$(ERR)
                                GOTO Terminate
                             END IF
                          ...
                          ...
                             vOptions = 1024
                             oTargetFolder.CopyHere(oItems, vOptions)
                          
                           lNr       = 0
                           lOutFiles = 0
                           DO
                             SLEEP 125
                             lOutFiles = oTargetFolder.Items().COUNT
                             IF ERR THEN
                                MSGBOX "error during the Count property in TargetFolder",,"Error:" & STR$(ERR)
                                GOTO Terminate
                             END IF
                             INCR lNr
                           LOOP UNTIL lNr > 240 OR lOutFiles >= lInFiles  ' max.30 Sec timeout, ">="  there can be any files at "unzip"
                           IF lNr => 240 THEN
                                MSGBOX "timeout during CopyHere method",,"Error"
                                GOTO Terminate
                           END IF
                          ...
                          Much fun

                          P.S. For example, I have taken 30 seconds timeout.
                          But it can and longer lasts at unzip operation... then change it.
                          Last edited by Alexander Holzer; 13 Nov 2008, 06:23 AM.
                          Yours sincerely

                          Comment


                          • #14
                            I had no problems zipping one file at a time with anywhere from five to over ninety files in this: Source Code Bundler v 3 handles PB 7/8/9/10 and CC 3/4/5/6 source code

                            (Yes, the routine is 'cut and paste' reuseable)

                            MCM
                            Michael Mattias
                            Tal Systems Inc. (retired)
                            Racine WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              I took the odd approach to embed 7zip into my exe and extract it in a tempfolder (and delete it afterwards).
                              Execution is hidden..
                              To bad is that it is rather big.
                              hellobasic

                              Comment

                              Working...
                              X