• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

Unzipping archive from Ruby (Mac and Windows)

Scheduled Pinned Locked Moved Developers' Forum
55 Posts 11 Posters 9.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    fredo6
    last edited by fredo6 8 Nov 2010, 11:54

    I am not sure this has not been covered elsewhere in the forum, but here is what I found for unzipping archives from Ruby.

    On Mac, it seems that there is a unzipcommand embedded.

    Here is a simple code snippet

    
    #Extract all files and sub-folders from an archive file to a destination folder
    #Status is returned true if successful, false otherwise
    
    def unzip_on_mac(archive, destination_folder)
      return false unless FileTest.exist?(archive)
      status = system "unzip -o \"#{archive}/*\" -d \"#{destination_folder}\""
    end
    
    

    A few notes

    • the internal double quotes are needed when archive or folder names include spaces
    • the flag -o is required to overwrite existing files if any
    • the operation is quite fast

    On Windows, it is more complex. It seems that all versions of Windows include a built-in facility for zipping and unzipping, but I did not find a way to invoke it from the command line. Also, the command Extract seems to work only for .cab files, not for .zip files. So you have to rely on an external zip program.

    1. if Winzip is installed, then its location should be
      zip_program= "C:/Program Files/Winzip/Winzip32" (or 'Program Files (x86)')
      Then the command to execute is simple
    
    def unzip_with_winzip(archive, destination_folder)
      system "#{zip_program} -min -e -o \"#{archive}\" \"#{destination_folder}\""
    end
    
    

    Note that:

    • -min is to avoid the nasty black screen popup
    • -e is just form extracting
    • -o is for overwriting all files if already present at destination
    1. we could similarly find typical location and command line parameters for other popular unzip programs(like 7zip or wintar)

    2. If none of them are present or can be located, then it is possible to download a small unzip.exe, which is just the part for unzipping of winzip (it is free and distributable). Its interest is that it is standalone (no dll dependency). It can be put in a SU plugin subfolder that is controlled and known.
      rename it unzip.exe
      in that case, assuming you put the path of the exe in variable zip_program, the command is

    
    def unzip_with_unzip(archive, destination_folder)
      system "#{zip_program} \"#{archive}\" -d \"#{destination_folder}\""
    end
    
    

    Note: this unzip program can be downloaded by users with a text extension, and then it is possible to rename it from Ruby, via File.rename

    Finally, on a related subject, I found that if urlpath contains the shortcut to a file attachment on Sketchucation (obtained via copy shortcut), then you can easily invoke the dialog box for download:

    
      urlpath = "http://forums.sketchucation.com/download/file.php?id=58102"
      wdlg = UI;;WebDialog.new
      wdlg.set_url urlpath
      wdlg.show
    
    

    The issue there is that you have no idea where the user has decided to put the file, or whether s/he has chosen to 'run' the file instead of saving.

    So I guess we'll need good ideas on how to download a remote file from the web, whether from Ruby or via a program launchable from the command line, without the many hassles of installing big side libraries (like net/http).

    Fredo

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 8 Nov 2010, 12:39

      If the user has install
      @unknownuser said:

      The issue there is that you have no idea where the user has decided to put the file, or whether s/he has chosen to 'run' the file instead of saving.

      So I guess we'll need good ideas on how to download a remote file from the web, whether from Ruby or via a program launchable from the command line, without the many hassles of installing big side libraries (like net/http).

      Webdialog - use XMLHttpRequest object to download the file. Though, if you try to download from SCF you need to be logged in, don't you?

      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by 8 Nov 2010, 13:49

        TBD's Dezip works great on SketchUp on Windows. I use it in a prototype plugin installer on my machine.

        Link Preview Image
        GitHub - TBD/OpenSUP: Open SketchUp Projects

        Open SketchUp Projects. Contribute to TBD/OpenSUP development by creating an account on GitHub.

        favicon

        GitHub (github.com)

        Hi

        1 Reply Last reply Reply Quote 0
        • T Offline
          tbd
          last edited by 8 Nov 2010, 14:01

          by default, on Mac, you cannot view .zip files as directories 😞
          and searching for installed programs to unzip is ugly and hard to debug/support.

          a port of Dezip to Mac would be nice to have 💚

          SketchUp Ruby Consultant | Podium 1.x developer
          http://plugins.ro

          1 Reply Last reply Reply Quote 0
          • J Offline
            jeff hammond
            last edited by 8 Nov 2010, 14:01

            not sure if this matters or not on what you're hoping to accomplish but just a heads up..

            when unarchiving on mac, there will be an extra folder..

            let's say i have a su plugin which consists of MyRuby.rb & an accompanying folder named MyRuby

            i compress those into a zip called Archive.zip

            when i unzip that, the structure will be:

            Archive (folder)

            MyRuby.rb
            MyRuby

            i think this is why many people end up with suPlugin install problems on mac because the instructions are always 'unzip in the plugin folder' which, of course, won't work due to that extra folder.

            [which can get more confusing when the .zip is named the same thing as the accompanying folder as it often is because you end up with two folders sharing the same name]

            dotdotdot

            1 Reply Last reply Reply Quote 0
            • T Offline
              tfdesign
              last edited by 8 Nov 2010, 16:32

              @unknownuser said:

              by default, on Mac, you cannot view .zip files as directories 😞

              I should think so too! How many PC's get infected by zips opening without permission? My sister has already had to reinstall her Windows OS because of similar mishaps (she is one of so many who could really know more about what they are doing!)

              @unknownuser said:

              and searching for installed programs to unzip is ugly and hard to debug/support.

              a port of Dezip to Mac would be nice to have 💚

              Really? Perhaps I didn't understand you? I just right click on a zipped download and a contextual menu appears with a list of options to choose from (including the built-in dearchiving software, which is activated by double clicking a zipped file anyway). I hardly call that ugly. 😕

              As for a decent zip utility, I thoroughly recommend Betterzip

              http://macitbetter.com/

              My book "Let's SketchUp!" Download from here

              1 Reply Last reply Reply Quote 0
              • T Offline
                thomthom
                last edited by 8 Nov 2010, 17:02

                @tfdesign said:

                How many PC's get infected by zips opening without permission?

                None. Opening up a zip is like opening up a folder - you don't execute any code.

                @tfdesign said:

                Really? Perhaps I didn't understand you? I just right click on a zipped download and a contextual menu appears with a list of options to choose from (including the built-in dearchiving software, which is activated by double clicking a zipped file anyway). I hardly call that ugly. 😕

                We're talking about unzipping via SketchUp Ruby in order to make installing and updating plugins easier. What he referred to as ugly was relying and detecting third party zipping software to do so.

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • F Offline
                  fredo6
                  last edited by 8 Nov 2010, 17:44

                  @thomthom said:

                  Though, if you try to download from SCF you need to be logged in, don't you?

                  Tom,

                  Very good remark, full of good sense.

                  Do you know if there is a way to specify the login on the flyin a request to a page (like http://forums.sketchucation.com/viewtopic.php?f=323&t=29230&hilit=plugin#p254673 )?

                  fredo

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 8 Nov 2010, 17:47

                    You could maybe send a POST message with XMLHttpRequest with login information to SCF's login page. ...though I'm not sure if doing that via XMLHttpRequest will give you a persistent session...

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      tfdesign
                      last edited by 8 Nov 2010, 17:54

                      @thomthom said:

                      @tfdesign said:

                      How many PC's get infected by zips opening without permission?

                      None. Opening up a zip is like opening up a folder - you don't execute any code.

                      That's not what I meant, but nevermind. I understand the context now 😄

                      @thomthom said:

                      @tfdesign said:

                      Really? Perhaps I didn't understand you? I just right click on a zipped download and a contextual menu appears with a list of options to choose from (including the built-in dearchiving software, which is activated by double clicking a zipped file anyway). I hardly call that ugly. 😕

                      We're talking about unzipping via SketchUp Ruby in order to make installing and updating plugins easier. What he referred to as ugly was relying and detecting third party zipping software to do so.

                      Okay 😄 It is different, but then I hardly use PC's any more, so I'm just used to the Mac way. 😄

                      My book "Let's SketchUp!" Download from here

                      1 Reply Last reply Reply Quote 0
                      • F Offline
                        fredo6
                        last edited by 8 Nov 2010, 20:27

                        @thomthom said:

                        You could maybe send a POST message with XMLHttpRequest with login information to SCF's login page. ...though I'm not sure if doing that via XMLHttpRequest will give you a persistent session...

                        Would anybody know:

                        1. either the inline syntax in the request (if this is supported), like
                          http://forums.sketchucation.com/viewtopic.php?f=323&t=29230&hilit=plugin#p254673 &user=Fredo6&pwd=mypwd

                        2. or the post syntax for the SCF login page?

                        Thanks

                        Fredo

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by 8 Nov 2010, 20:35

                          What URL do you see if you hover over the SCF Login link.
                          (I have autologin set in my SCF cookie so I never go thru the login.)

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • J Offline
                            jeff hammond
                            last edited by 8 Nov 2010, 20:44

                            @dan rathbun said:

                            What URL do you see if you hover over the SCF Login link.
                            (I have autologin set in my SCF cookie so I never go thru the login.)

                            the log in page at scf is this url:

                            http://forums.sketchucation.com/ucp.php?mode=login
                            

                            dotdotdot

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 8 Nov 2010, 20:44

                              It would not be part of the URL - as that would be a GET.

                              The HTML source for the login:

                              
                              <form method="post" action="./ucp.php?mode=login" class="headerspace" style="border;dashed 2px #ca0016;margin;auto auto 10px auto;padding;0 10px 10px 10px;width;97%;">
                              	<p style="font-weight;bold;margin;11px 0 0 0;float;right;padding;3px 5px;"><img src="http://sketchucation.com/forums/styles/prosilver/imageset/help.gif" height="22" width="23" alt="" style="margin-right;5px;" /><a href="http://www.sketchucation.com/scf-contact-page2/" style="font-size;1.1em;">Problems with registering / logging in?</a></p>
                              	<h3><a href="./ucp.php?mode=login">Login</a>&nbsp; &bull; &nbsp;<a href="./ucp.php?mode=register">Register</a></h3>
                              		<fieldset class="quick-login">
                              			<label for="username">Username;</label>&nbsp;<input type="text" name="username" id="username" size="10" class="inputbox" title="Username" />  
                              			<label for="password">Password;</label>&nbsp;<input type="password" name="password" id="password" size="10" class="inputbox" title="Password" />
                              			
                              				| <label for="autologin">Log me on automatically each visit <input type="checkbox" name="autologin" id="autologin" /></label>
                              			
                              			<input type="submit" name="login" value="Login" class="button2" />
                              		</fieldset>
                              	</form>
                              
                              

                              The important stuff here is:
                              <form method="post" action="./ucp.php?mode=login"

                              <input type="text" name="username" id="username" size="10" class="inputbox" title="Username" />
                              <input type="password" name="password" id="password" size="10" class="inputbox" title="Password" />
                              <input type="submit" name="login" value="Login" class="button2" />

                              This means the POST request must be made to http ://forums.sketchucation.com/ucp.php?mode=login
                              and you need to set the arguments, "username", "password" and quite possible "login" (login as very often the website software checks the value of the submit button to detect if a form has been used - but that depends on the software)

                              Thomas Thomassen — SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by 8 Nov 2010, 20:46

                                The form fieldnames on the SCF login page are:
                                username
                                password

                                But there is also a "sid" (security ID) involved. The Admins may not want spambots being able to auto login. Ask Coen.

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • T Offline
                                  thomthom
                                  last edited by 8 Nov 2010, 20:47

                                  @dan rathbun said:

                                  What URL do you see if you hover over the SCF Login link.
                                  (I have autologin set in my SCF cookie so I never go thru the login.)

                                  HTML forms' submit buttons doesn't display the target URL info. One has to look at the source code.

                                  Thomas Thomassen — SketchUp Monkey & Coding addict
                                  List of my plugins and link to the CookieWare fund

                                  1 Reply Last reply Reply Quote 0
                                  • Dan RathbunD Offline
                                    Dan Rathbun
                                    last edited by 8 Nov 2010, 20:49

                                    @thomthom said:

                                    One has to look at the source code.

                                    I know (and I did,) it was a hint. But you cut and pasted the code before I could.

                                    I'm not here much anymore.

                                    1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      tbd
                                      last edited by 8 Nov 2010, 21:41

                                      Fredo6: what do you want to do with SCF download ? maybe we can create a backend that suits your needs better thank going through forum software

                                      SketchUp Ruby Consultant | Podium 1.x developer
                                      http://plugins.ro

                                      1 Reply Last reply Reply Quote 0
                                      • F Offline
                                        fredo6
                                        last edited by 8 Nov 2010, 21:57

                                        @unknownuser said:

                                        Fredo6: what do you want to do with SCF download ? maybe we can create a backend that suits your needs better thank going through forum software

                                        I am investiogating 2 things:

                                        1) Scan the SCF page where a plugin is.
                                        That's sorted out with WebDialog and this will allow to check the current version and date of the plugin. This of course requires some strict syntax convention, for instance in the title, so that it is easy and discriminant to parse the info.
                                        At least, this will tell whether the plugin is up to date or not

                                        2) Download the file (zip or rb) to the local disk in the right place.
                                        I assume that with some syntax convention, it will be possible to locate the URL address of the file.
                                        For this however, this requires that the user is logged in to SCF (as ThomThom judiciously noticed).
                                        Then, if the file URL is obtained, I can:

                                        • either invoke the download dialog box via WebDialog
                                        • or find out another way to download it
                                          Not sure there is an easy way

                                        Fred

                                        1 Reply Last reply Reply Quote 0
                                        • F Offline
                                          fredo6
                                          last edited by 8 Nov 2010, 22:01

                                          I was WRONG about the Mac unzipping as folder.

                                          But I found out that there is an embedded unzip command, which can be invoked via the Ruby system call system.
                                          see main post for details

                                          I have no clue if this is present on all Macs.

                                          Fredo

                                          PS: just taking the opportunity that I have a Mac at hand to make a few tests about this question of plugin installation, as I usually prefer to release scripts for both environments and users.

                                          1 Reply Last reply Reply Quote 0
                                          • 1
                                          • 2
                                          • 3
                                          • 1 / 3
                                          1 / 3
                                          • First post
                                            1/55
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement