How to zip files?


#1

I have exported the model in obj format. I want to upload all the exported files to my server.
For that I want to create the zip file from exported files.
How can i create zip file using Sketchup api or use system command within ruby ?


#2

I’ve used Rubyzip for this. It’s written 100% in Ruby and can quite easily be modified to be defined withing the namespace of your extension.


#3

@rajawat, It is a gem.

Gem::install('rubyzip')
require "zip"

Documentation:

http://www.rubydoc.info/github/rubyzip/rubyzip

Example:

begin
  require 'zip'
rescue
  Gem::install('rubyzip')
  require 'zip'
end

def zip_my_files(
  export_dir = "C:/some/path/to/export/files",
  zip_dir  = "C:/some/path/to/export/",
  zip_name = "export.zip"
)
  zip_path = File.join( zip_dir, zip_name )

  Zip::File.open(zip_path, Zip::File::CREATE) {|zipfile|
    Dir::chdir(export_dir) {
      files = Dir["*.*"]
      files.each do |file|
        zipfile.add( file, File.expand_path(file) )
      end
    }
  }
end

:smile:


#4

I first tried installing the Gem on like this but it was incredible slow. For a minute or two SketchUp was completely unresponsive. Then I switched to just include it inside of my own extension, edited to be under my own namespace. Works like a charm.


#5

Sure it does, and if we all do this it wastes the user’s memory.

The whole point of shared libraries (like gems) is that we all use the same ones.

A gem install progress bar was logged as an issue I think several years ago.
(I even attempted to write one.)

We could inform the user that it will take a few minutes to load the gem, … but that usually means a model messagebox, which does not make sense during SketchUp startup (where dependencies like gems are installed.)

So, … it takes a few minutes more the first time an extension loads and installs RubyZip. Thereafter it is no longer an issue.


#6

The codebase is really small. Having 3 or 4 copies of it doesn’t hurt anyone. On the other hand there could be clashes if one extension requires a new version that an older extension no longer supports.

That’s still an issue. If SketchUp is unresponsive for more than a minute without giving any feedback the user is quite likely to think something is broken and kills the program.


#7

Feedback to the user is always a good thing (even during startup it might be nice to show the users that things are happening behind the GUI.)


#8

I tried

But then Sketchup went to “Not Responding” state for few minutes and then throws following error.
Error: #<LoadError: cannot load such file – zip>


#9

Is your internet connection on ?
If so, what kind of connection do you have ?


These Gem install errors are what Christina was speaking of above, and why she wraps RubyZip inside her extension module.

You can never be sure of how good or bad a user’s internet connection is.

A few years back I did a test and packaged up RubyZip as a SketchUp extension. It is a few version old by now.


#10

Speaking of that, an extension should be possible to install offline, e.g. from a flash drive. The only exception I can think of is when the licensing of a paid extension needs a connection to the licensing server. If the gem is installed as a gem it needs a bit of error handling and extra work on the UI to handle being offline.


#11

Well the gem package would need to have been previously downloaded into the gem "cache" subfolder (and perhaps maybe also the .gemspec file to the gem "specifications" subfolder,) at some earlier point in time whilst still online.

That beginrescue block above was just quick and dirty from memory.
But I have somewhere, an expanded one that traps specific Gem::InstallError exceptions, etc.
(It may have even attempted to check if the machine was online, which would be a smart thing to check first.)


#12

Or you just wrap the gem into the extension source and don’t worry about these things.


#13

Something as small as this perhaps, … but fundamentally it would be nice if the developers could rely upon the same libraries.

(It is weird that RubyZip has never been included in the Ruby standard library when other *nix type archive tools have.) ADD: Jim Foltz filed a FR years ago asking “the team” to include RubyZip in it’s "RubyStdLib" distribution folder.

When the gem is larger and more complex (and perhaps also contains binaries) this scenario is not so simple.


#14

Exactly. It’s common practice to include all dependencies in your package to be, well, less dependent.
node.js does this, .net core does this, …


#15

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.