sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Unzipping archive from Ruby (Mac and Windows)

    Scheduled Pinned Locked Moved Developers' Forum
    55 Posts 11 Posters 9.4k Views 11 Watching
    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.
    • eneroth3E Offline
      eneroth3
      last edited by

      Thanks! The gem rubyzip gem does exactly what I wanted 😄 .

      However I don't know what's the best practice to use a gem. Should I ask the user to install it or should I check if it exists when the plugin loads and otherwise install it? Should I in that case use statusbar texts to tell the user the gem is being installed and that it may take some time?

      My website: http://julia-christina-eneroth.se/

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by

        It only instals into Sketchup gems, so if they want your plugin they will need it...

        If it not already installed:
        I guess you could ask if they want the download or to use 'your' supplied copy, but you would need to keep 'yours' up to date as other plugins use it already...

        it's very fast to load, did you try?

        Definitely mention it in your documentation...

        john

        learn from the mistakes of others, you may not live long enough to make them all yourself...

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          There are 'built-in' zip and unzip methods in Ruby2 - using 'zlib', with no gems.
          See my ZipUp Plugin which uses its own shipped rubies to make things easier [Rubyzip].
          https://github.com/rubyzip/rubyzip
          Mine also uses some js and system zipping [MAC is easy, but on WIN is convoluted] when it's older SketchUp versions, < Ruby2...
          It's in an RBS, but suffice it to say that it uses the additional zip.rb loaded commands to make the Ruby2 'zlib' code much less cumbersome to use...

          TIG

          1 Reply Last reply Reply Quote 0
          • eneroth3E Offline
            eneroth3
            last edited by

            It took me 69,12 seconds to install the gem :S . I think I have a quite good Internet connection, at least a decent one, but it might be my dying hard drive.

            I still suppose it's better to download the most current version than rely on something I pack with the plugin. If I pack it with my files I suppose it has to be in my own namespace to avoid collisions if another plugin requires a newer version.

            Perhaps there should be a web dialog telling the user the gem is being installed so they know Sketchup hasn't just frozen.

            My website: http://julia-christina-eneroth.se/

            1 Reply Last reply Reply Quote 0
            • D Offline
              driven
              last edited by

              it really depends on what your zipping/unzipping...
              for mac the system zip and unzip are the cleanest and fastest...
              I use it on .skm's and .layout's mainly.
              for .rbz I use Sketchup.install_from_archive and let SU move the files...

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              1 Reply Last reply Reply Quote 0
              • eneroth3E Offline
                eneroth3
                last edited by

                TIG: I experimented a little with Zlib before but I couldn't find a way to save multiple files in the same archive which I need for my plugin, it doesn't seem to be supported.

                My website: http://julia-christina-eneroth.se/

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  Mine only loads zlib.so and requires the ruby zip.rb stuff if it's not already been installed.
                  So if another plugin already installed it it uses that.
                  You can of course so it entirely in vbs/mac-shell for all versions....
                  These are all inside begin/rescue/ensure sections...
                  ` require('zlib') unless defined?(Zlib)

                  in case it is already loaded, and in case it fails it's in a 'rescue'`

                  To allow my require('zip') to work I briefly add the path to the plugins own subfolder into $: thus:
                  ` ...
                  rubyzip=File.join(DATA, 'Rubyzip')

                  where DATA is a constant referring to that plugin's subfolder named 'Rubyzip'

                  $: << rubyzip
                  ...
                  require('zip') unless defined?(Zip)

                  in case not already installed by another plugin...

                  ...
                  $:.delete(rubyzip)

                  to tidy up, ad it's no longer needed...

                  ...`

                  The Rubyzip methods allow you to add multiple files into a ZIP file - in fact you can do most ZIP/unZIP things with it in conjunction with zlib...

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    Using Rubyzip...

                    zipped = Zip;;File.open(zip, Zip;;File;;CREATE) do |zipfile|
                      # - The name of the file as it will appear in the archive
                      # - The original file, including the path to find it
                      zipfile.add(skp, tempskp)
                      zipfile.add(txt, temptxt)
                    end
                    

                    This opens a ZIP file, whose full-path ref is 'zip' [in this case a new one].
                    It then adds a file to that ZIP, whose full-path ref is in 'tempskp', and which will appear in the ZIP as the reference 'skp' - in this case it's the SKP's name...
                    It then adds another file to the ZIP, whose full-path ref is in 'temptxt', and which will appear in the ZIP as the reference 'txt' - in this case it's the TXT file's name...
                    You can also add subfolders etc as desired - read the Rubyzip usage notes...

                    You don't need to install the 'gem' for the users - just look how I am pre-including them in ../PluginsZipUp/Data/Rubyzip ... with the RBZ installer...

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • D Offline
                      driven
                      last edited by

                      @tig
                      how do you 'test' a zipped folder with Rubyzip?

                      for example with unzip you have the -t flag...

                            def is_texture(file)
                              %x( unzip -t "#{file}"  ).include? "ref\/.*g" # file = path to skm and ref/ means it has a texture folder
                            end
                      

                      learn from the mistakes of others, you may not live long enough to make them all yourself...

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        Yours is the simple MAC way !
                        You can find/read the contents of an existing ZIP for a match in this kind of example...

                        Zip;;File.open(path_to_some_foo_zip') do |zip_file|
                          # Handle entries one by one
                          zip_file.each do |entry|
                            # Extract to file/directory/symlink
                            puts "Extracting #{entry.name}"
                            entry.extract(dest_file)
                            # Read into memory
                            content = entry.get_input_stream.read
                          end
                          # Find specific entry
                          entry = zip_file.glob('*.csv').first
                          puts entry.get_input_stream.read
                        end
                        

                        You need to set up your reference variables...

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • eneroth3E Offline
                          eneroth3
                          last edited by

                          These examples use rubyzip but from a subdirectory rather than a Gem. Is it possible to only use the Zlib without rubyzip to put multiple files in the same zip archive?

                          I use these archives to store information for what can be drawn with my plugin. In my previous plugin I stored railroad tracks in a similar way but in their own folders instead of archives. Each folder contained a few .skp models for things to draw when drawing the track, e.g profiles, a preview image for web dialogs and a text file containing some information dynamically displayed in the web dialogs without loading the models and littering the definition list.

                          In this project I want to store all that data in one single file to make it easier for users to exchange them. I've already managed to do that but I don't know what's considered best practice, installing a Gem or copy the code into my own plugin's folder.

                          My website: http://julia-christina-eneroth.se/

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            driven
                            last edited by

                            have you tried this...

                            system( 'ROBOCOPY', files_directory, new_zip_folder_path )
                            

                            if it works [I don't have a PC to test], The mac version is almost the same...

                            system('zip', '-r', new_zip_folder_path,  files_directory)
                            

                            john

                            learn from the mistakes of others, you may not live long enough to make them all yourself...

                            1 Reply Last reply Reply Quote 0
                            • TIGT Offline
                              TIG Moderator
                              last edited by

                              Your MAC ' zip' example is much like I use on older SketchUp version MACs with no Ruby zip built-in [with -q ?].

                              Your ' ROBOCOPY' examples just makes a copy of the files in the source-folder in a new folder named ' xxxx.zip' - not zipped ! and it flashes up a black cmd window - just like in any WIN system call.

                              On PCs you can make a longer set of strings that are VB commands. and put them into a ' xxxx.vbs' file which then runs silently using UI.openURL("file:///#{path_to_vbs_file}"), and it will make a proper ZIP file... I use that in older SketchUp version PCs with no Ruby zip built-in.

                              txt="Const FOF_CREATEPROGRESSDLG = &H0&
                              Const MyZip = \"#{zip.tr("/","\\")}\"
                              Const File1 = \"#{tskp1.tr("/","\\")}\"
                              Const File2 = \"#{tskp2.tr("/","\\")}\"
                              'Create the basis of new zip file
                              CreateObject(\"Scripting.FileSystemObject\").CreateTextFile(MyZip, True).Write \"PK\" & Chr(5) & Chr(6) & String(18, vbNullChar)
                              'get ready to add files to zip
                              With CreateObject(\"Shell.Application\")
                              'add files
                              .NameSpace(MyZip).CopyHere File1, FOF_CREATEPROGRESSDLG
                              wScript.Sleep 200
                              .NameSpace(MyZip).CopyHere File2
                              End With
                              ' wait 3 secs, to let it finish...
                              wScript.Sleep 3000
                              '''
                              

                              Where ' zip' is the path to the new ZIP file, and ' tskp1' etc paths to SKP files - the tr makes / into \ for VBS use
                              You can get it to write a temp txt file at the end so you know it's done...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • fredo6F Offline
                                fredo6
                                last edited by

                                Yes, I noticed there is a Zip module that seems to ship in Ruby 2.0 in the standard Sketchup installation.

                                I did not find the documentation however, but this could be the answer, at least for SU2014 and SU2015.

                                Fredo

                                1 Reply Last reply Reply Quote 0
                                • D Offline
                                  driven
                                  last edited by

                                  301 Moved Permanently

                                  favicon

                                  (ruby-doc.org)

                                  learn from the mistakes of others, you may not live long enough to make them all yourself...

                                  1 Reply Last reply Reply Quote 0
                                  • tt_suT Offline
                                    tt_su
                                    last edited by

                                    @eneroth3 said:

                                    Thanks! The gem rubyzip gem does exactly what I wanted 😄 .

                                    However I don't know what's the best practice to use a gem. Should I ask the user to install it or should I check if it exists when the plugin loads and otherwise install it? Should I in that case use statusbar texts to tell the user the gem is being installed and that it may take some time?

                                    What I've done is wrap the require in a begin/rescue that catch LoadErrors (you don't want to catch other errors) and then use Gem.install to install it if it's missing.

                                    1 Reply Last reply Reply Quote 0
                                    • tt_suT Offline
                                      tt_su
                                      last edited by

                                      @fredo6 said:

                                      Yes, I noticed there is a Zip module that seems to ship in Ruby 2.0 in the standard Sketchup installation.

                                      I did not find the documentation however, but this could be the answer, at least for SU2014 and SU2015.

                                      Fredo

                                      It doesn't support ZIP files, it for general compression and GZ files.

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        driven
                                        last edited by

                                        cheers for the explanation...

                                        as I follow up to your last reply, this works on mac...

                                              def is_texture(file)
                                                  result = false
                                                  Zip;;InputStream;;open(file) {|io|
                                                    while (entry = io.get_next_entry)
                                                      result = true if entry.to_s.include? "ref\/"
                                                    end
                                                  result
                                                  }
                                              end
                                        

                                        could you test on Win with a skm path as file...
                                        john
                                        EDIT: added result = false although it is working...

                                        learn from the mistakes of others, you may not live long enough to make them all yourself...

                                        1 Reply Last reply Reply Quote 0
                                        • fredo6F Offline
                                          fredo6
                                          last edited by

                                          @driven said:

                                          301 Moved Permanently

                                          favicon

                                          (ruby-doc.org)

                                          This is ZLib. Is the Zip module just a wrapper of it?

                                          Fredo

                                          1 Reply Last reply Reply Quote 0
                                          • Dan RathbunD Offline
                                            Dan Rathbun
                                            last edited by

                                            @driven said:

                                            could you test on Win with a skm path as file...
                                            john

                                            John the following is what I will have used in my SKM Import Library:
                                            (Yes it works on Windows and should also on Mac.)

                                            <span class="syntaxdefault">  def skm_has_texture</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">file_path</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment"># validate file_path here<br /></span><span class="syntaxdefault">    bool </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br />    Zip</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">open</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">file_path</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">skm</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">      tag </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> skm</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">read</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'document.xml'</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">slice</span><span class="syntaxkeyword">(/<</span><span class="syntaxdefault">mat</span><span class="syntaxkeyword">;</span><span class="syntaxdefault">material</span><span class="syntaxkeyword">\</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">(.*)(\</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">*>|\</span><span class="syntaxdefault">s</span><span class="syntaxkeyword">*\/>)/)<br /></span><span class="syntaxdefault">      unless tag</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">        bool </span><span class="syntaxkeyword">=(</span><span class="syntaxdefault"> tag </span><span class="syntaxkeyword">!~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/(</span><span class="syntaxdefault">hasTexture</span><span class="syntaxkeyword">=\</span><span class="syntaxstring">"0\")/ rescue false )<br />      else<br />        puts("</span><span class="syntaxdefault">XML Read Error</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> Could not extract </span><span class="syntaxkeyword">\</span><span class="syntaxstring">"mat;material\" element.>"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">      end<br />    end </span><span class="syntaxcomment"># open skmfile<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault">  rescue </span><span class="syntaxkeyword">=></span><span class="syntaxdefault"> e<br />    puts</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Error; #<#{e.class.name}; #{e.message}.>"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    return nil<br />  else<br />    return bool<br />  end </span><span class="syntaxcomment"># skm_has_texture?()<br />&nbsp;</span><span class="syntaxdefault"></span>
                                            

                                            I'm not here much anymore.

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

                                            Advertisement