PageRenderTime 77ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/2013/10/01/ruby_reading_a_remote_zip_file/index.html

https://github.com/winston/winston.github.com
HTML | 143 lines | 128 code | 14 blank | 1 comment | 0 complexity | b02c607654ad7ddb52a3ccda3be325d9 MD5 | raw file
Possible License(s): MIT
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Ruby - Reading A Remote Zip File - WinstonYW</title>
  5. <meta content="text/html; charset=utf-8" http-equiv="content-type" />
  6. <meta content='Winston Teo Yong Wei' name='author' />
  7. <link href='/assets/css/application.css' media='screen' rel='stylesheet' type='text/css' />
  8. <!-- http://davidbcalhoun.com/2010/viewport-metatag -->
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <meta name="HandheldFriendly" content="True">
  11. <meta name="MobileOptimized" content="320">
  12. </head>
  13. <body>
  14. <header>
  15. <div class='title-color-bar'></div>
  16. <section id='profile'>
  17. <h1>
  18. <a href="/">WinstonYW</a>
  19. </h1>
  20. <h2>
  21. Just Another Tech Journal
  22. </h2>
  23. </section>
  24. <div class='aside-color-bar'></div>
  25. <section id="projects">
  26. <h2>Projects</h2>
  27. <ul>
  28. <li>
  29. <a href="https://github.com/winston/google_visualr">GoogleVisualr</a>
  30. </li>
  31. <li>
  32. <a href="https://github.com/winston/cactus">Cactus</a>
  33. </li>
  34. <li>
  35. <a href="http://gigest.herokuapp.com">Gigest App</a>
  36. </li>
  37. </ul>
  38. </section>
  39. <section id="elsewhere">
  40. <h2>Elsewhere</h2>
  41. <ul>
  42. <li>
  43. <a href="https://twitter.com/winstonyw">Twitter</a>
  44. </li>
  45. <li>
  46. <a href="https://github.com/winston">GitHub</a>
  47. </li>
  48. <li>
  49. <a href="http://www.linkedin.com/in/winstonyw">LinkedIn</a>
  50. </li>
  51. <li>
  52. <a href="mailto:winstonyw+blog@gmail.com">Email</a>
  53. </li>
  54. <li>
  55. <a href="#">RSS</a>
  56. </li>
  57. </ul>
  58. </section>
  59. </header>
  60. <div id='center'>
  61. <div class='color-bar'></div>
  62. <div id='content'>
  63. <article class="post">
  64. <h2>Ruby - Reading A Remote Zip File</h2>
  65. <span class="date">01 OCTOBER 2013</span>
  66. <section>
  67. <p>I need to access a remote zip file and this is something that works:</p>
  68. <div class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># In the console, gem install &quot;httparty&quot;</span>
  69. <span class="nb">require</span> <span class="s2">&quot;httparty&quot;</span>
  70. <span class="c1"># In the console, gem install &quot;rubyzip&quot;</span>
  71. <span class="nb">require</span> <span class="s2">&quot;zip&quot;</span>
  72. <span class="c1"># Get the contents of the remote zip file via HTTParty</span>
  73. <span class="c1"># and write it into a temp zip file</span>
  74. <span class="n">zipfile</span> <span class="o">=</span> <span class="no">Tempfile</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;file&quot;</span><span class="p">)</span>
  75. <span class="n">zipfile</span><span class="o">.</span><span class="n">binmode</span> <span class="c1"># This might not be necessary depending on the zip file</span>
  76. <span class="n">zipfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="no">HTTParty</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;http://localhost:3000/file.zip&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">body</span><span class="p">)</span>
  77. <span class="n">zipfile</span><span class="o">.</span><span class="n">close</span>
  78. <span class="c1"># Unzip the temp zip file and process the contents</span>
  79. <span class="c1"># Let garbage collection delete the temp zip file</span>
  80. <span class="no">Zip</span><span class="o">::</span><span class="no">File</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">zipfile</span><span class="o">.</span><span class="n">path</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">file</span><span class="o">|</span>
  81. <span class="n">file</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">content</span><span class="o">|</span>
  82. <span class="n">data</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
  83. <span class="nb">puts</span> <span class="n">data</span>
  84. <span class="c1"># Do whatever you want with the contents</span>
  85. <span class="k">end</span>
  86. <span class="k">end</span></code></pre></div>
  87. <p>The code is simple, but at the start, I kept getting an error when unzipping the temp zip file,
  88. and I thought I was doing something wrong.</p>
  89. <pre><code>End-of-central-directory signature not found
  90. </code></pre>
  91. <p>Did some debugging and finally figured out that the problem was with the remote zip file
  92. - because the file was not fully constructed even though I had a link to it.</p>
  93. <p>The remote zip file link was actually returned by an earlier API call to an external service
  94. that also triggered the building of the remote zip file.</p>
  95. <p>Moral of the story? Trust my code.</p>
  96. <p>Anyway, <a href="http://blog.huangzhimin.com/2012/10/02/avoid-using-rubyzip/">RubyZip is poor in performance</a>. Might want to try <a href="http://zipruby.rubyforge.org/">ZipRuby</a> instead.</p>
  97. </section>
  98. </article>
  99. <article class="comments">
  100. <div id="disqus_thread"></div>
  101. <script type="text/javascript">
  102. var disqus_shortname = 'winstonyw';
  103. // var disqus_developer = 1;
  104. (function() {
  105. var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
  106. dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
  107. (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  108. })();
  109. </script>
  110. <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
  111. <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
  112. </article>
  113. </div>
  114. </div>
  115. <footer>
  116. Just a boring footer. Nothing to see here lah. ~
  117. </footer>
  118. <script type="text/javascript">
  119. var _gaq = _gaq || [];
  120. _gaq.push(['_setAccount', 'UA-297001-2']);
  121. _gaq.push(['_trackPageview']);
  122. (function() {
  123. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  124. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  125. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  126. })();
  127. </script>
  128. </body>
  129. </html>