<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: file code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:44:00 GMT</pubDate>
    <description>DZone Snippets: file code</description>
    <item>
      <title>file extension methods</title>
      <link>http://snippets.dzone.com/posts/show/5509</link>
      <description>Allows you to use file extensions as methods&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class File&lt;br /&gt;  # Feel free to add more here, as you need them.&lt;br /&gt;  Extensions = %r=^(txt|rb|markdown|textile|haml|sass|css|html|xhtml)$=i&lt;br /&gt;  &lt;br /&gt;  module Extension&lt;br /&gt;    def method_missing(meth, *args)&lt;br /&gt;      if Extensions =~ meth.to_s&lt;br /&gt;        [self, '.', meth.to_s].join&lt;br /&gt;      else&lt;br /&gt;        super&lt;br /&gt;      end # if&lt;br /&gt;    end # method_missing&lt;br /&gt;  end # Extension&lt;br /&gt;end # File&lt;br /&gt;&lt;br /&gt;class Symbol&lt;br /&gt;  include File::Extension&lt;br /&gt;end&lt;br /&gt;class String&lt;br /&gt;  include File::Extension&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;'myfile'.html&lt;br /&gt;# =&gt; "myfile.html"&lt;br /&gt;&lt;br /&gt;:a_file.rb&lt;br /&gt;# =&gt; "a_file.rb"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 17 May 2008 02:08:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5509</guid>
      <author>elliottcable (elliott cable)</author>
    </item>
    <item>
      <title>perl grep dir and filename</title>
      <link>http://snippets.dzone.com/posts/show/5492</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my($directory, $filename) = $text =~ m/(.*\/)(.*)$/;&lt;br /&gt;print "D=$directory, F=$filename\n&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 14 May 2008 20:03:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5492</guid>
      <author>counter202 (jensen)</author>
    </item>
    <item>
      <title>Simple File.find</title>
      <link>http://snippets.dzone.com/posts/show/5457</link>
      <description>&lt;code&gt;&lt;br /&gt;# Simple File.find by c00lryguy&lt;br /&gt;# Thanks to justinwr for adding what I forgot to do&lt;br /&gt;# ------------------------------&lt;br /&gt;# Usage: &lt;br /&gt;#     * = wildcard in filename&lt;br /&gt;#   File.find("E:\\") =&gt; All files in E:\&lt;br /&gt;#   File.find("E:\\Ruby", "*.rb") =&gt; All .rb files in E:\Ruby&lt;br /&gt;#   File.find("E:\\", "*.rb", false) =&gt; All .rb files in E:\, but not in its subdirs&lt;br /&gt;class File&lt;br /&gt;  def self.find(dir, filename="*.*", subdirs=true)&lt;br /&gt;    Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 03 May 2008 18:56:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5457</guid>
      <author>c00lryguy (c00lryguy@gmail.com)</author>
    </item>
    <item>
      <title>IP Catcher</title>
      <link>http://snippets.dzone.com/posts/show/5430</link>
      <description>--Howto use--&lt;br /&gt;the command line:&lt;br /&gt;   ruby /path/to/ipcatcher.rb /path/to/filename&lt;br /&gt;the program will print all the ip addresses which is inside the file.&lt;br /&gt; the program doesn't print the same ip address twice. with no duplications.&lt;br /&gt;Done by &lt;a href="http://www.amerj.info"&gt;Amer Jazaerly&lt;/a&gt;.there is no copyright.&lt;br /&gt;have fun ;)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;def get_ips(file)&lt;br /&gt;  ips = []&lt;br /&gt;  File.read(file).to_a.each do |place|&lt;br /&gt;    sf = 0&lt;br /&gt;    while sfn = place.index(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/,sf)&lt;br /&gt;      sf = sfn + 3&lt;br /&gt;      ips &lt;&lt; $&amp;&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  return ips&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;get_ips(ARGV[0]).uniq.each { |ip| puts ip }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 26 Apr 2008 08:18:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5430</guid>
      <author>amer (Amer Jazaerly)</author>
    </item>
    <item>
      <title>nginx expires config</title>
      <link>http://snippets.dzone.com/posts/show/5386</link>
      <description>&lt;code&gt;&lt;br /&gt;  location ~* ^.+\.(js|css|php|jpg|jpeg|gif|png|pdf|zip|rar)$ {&lt;br /&gt;    root   /var/www/apps/myapp/public;&lt;br /&gt;    access_log   off;&lt;br /&gt;    expires      7d;&lt;br /&gt;  }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 18 Apr 2008 14:12:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5386</guid>
      <author>indiehead (John)</author>
    </item>
    <item>
      <title>Upload a file using Ajax</title>
      <link>http://snippets.dzone.com/posts/show/5166</link>
      <description>This code is categorised as Ajax because it "fits within my definition of Ajax" as explained from the &lt;a href="http://www.openjs.com/articles/ajax/ajax_file_upload/"&gt;Ajax File Upload&lt;/a&gt; [openjs.com] article.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;  &lt;head&gt;&lt;br /&gt;    &lt;title&gt;File upload&lt;/title&gt;&lt;br /&gt;    &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt;&lt;br /&gt;    &lt;script type="text/javascript"&gt;&lt;br /&gt;    //&lt;![CDATA[&lt;br /&gt;      function init() {&lt;br /&gt;	document.getElementById('file_upload_form').onsubmit=function() {&lt;br /&gt;                                  //'upload_target' is the name of the iframe&lt;br /&gt;	  document.getElementById('file_upload_form').target = 'upload_target'; &lt;br /&gt;	  }&lt;br /&gt;  }&lt;br /&gt;  window.onload=init;&lt;br /&gt;    //]]&gt;&lt;br /&gt;    &lt;/script&gt;&lt;br /&gt;  &lt;/head&gt;&lt;br /&gt;  &lt;body&gt;&lt;br /&gt;    &lt;form id="file_upload_form" method="post" enctype="multipart/form-data" action="/p/file_upload.cgi"&gt;&lt;br /&gt;    &lt;input name="myfile" id="myfile" size="27" type="file" /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;input type="submit" name="action" value="Upload" /&gt;&lt;br /&gt;&lt;br /&gt;    &lt;iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"&gt;&lt;/iframe&gt;&lt;br /&gt;    &lt;/form&gt;&lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 23 Feb 2008 14:33:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5166</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Upload a file using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5165</link>
      <description>The following code was used to upload an image file to the web server. Source code origin: &lt;a href="http://www.zytrax.com/tech/lang/ruby/#upload"&gt;Ruby Language Stuff | mod_ruby File upload scripts&lt;/a&gt; [zytrax.com]&lt;br /&gt;&lt;br /&gt;file: file_upload.cgi&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;# ruby script fragment&lt;br /&gt;require 'cgi'&lt;br /&gt;require 'stringio'&lt;br /&gt;&lt;br /&gt;cgi = CGI.new()  # New CGI object&lt;br /&gt;puts "Content-Type: text/plain"&lt;br /&gt;puts&lt;br /&gt;print '&lt;result&gt;'&lt;br /&gt;&lt;br /&gt;# get uri of tx'd file (in tmp normally)&lt;br /&gt;tmpfile = cgi.params['myfile'].first.path&lt;br /&gt;&lt;br /&gt;# OR (functionally the same)&lt;br /&gt;tmpfile = cgi.params['myfile'][0].path&lt;br /&gt;&lt;br /&gt;# create a Tempfile reference&lt;br /&gt;fromfile = cgi.params['myfile'].first&lt;br /&gt;&lt;br /&gt;#displays the original file name as supplied in the form&lt;br /&gt;puts fromfile.original_filename&lt;br /&gt;&lt;br /&gt;# displays the content (mime) type e.g. text/html&lt;br /&gt;puts fromfile.content_type&lt;br /&gt;&lt;br /&gt;# create output file reference as original filename in our chosen directory&lt;br /&gt;tofile = '/var/www/yourdomain.com/htdocs/r/'+fromfile.original_filename&lt;br /&gt;&lt;br /&gt;# copy the file&lt;br /&gt;# note the untaint prevents a security error&lt;br /&gt;# cgi sets up an StringIO object if file &lt; 10240&lt;br /&gt;# or a Tempfile object following works for both&lt;br /&gt;File.open(tofile.untaint, 'w') { |file| file &lt;&lt; fromfile.read}&lt;br /&gt;# when the page finishes the Tempfile/StringIO!) thing is deleted automatically&lt;br /&gt;&lt;br /&gt;print '&lt;/result&gt;'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;file: file_upload.html&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;  &lt;head&gt;&lt;br /&gt;    &lt;title&gt;File upload&lt;/title&gt;&lt;br /&gt;    &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt;&lt;br /&gt;  &lt;/head&gt;&lt;br /&gt;  &lt;body&gt;&lt;br /&gt;    &lt;form name='fileupload' enctype="multipart/form-data" &lt;br /&gt;    action='/p/file_upload.cgi' method='post'&gt;&lt;br /&gt;    &lt;input type='file' name='myfile' size="40" /&gt;&lt;br /&gt;    &lt;input type='submit' value"Send it"/&gt;&lt;br /&gt;    &lt;/form&gt;&lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;  &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 23 Feb 2008 13:26:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5165</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Find all subdirectories of a given path at a particular depth</title>
      <link>http://snippets.dzone.com/posts/show/5111</link>
      <description>&lt;code&gt;&lt;br /&gt;def subdirectories_of(path, options = {}) depth = options[:at_depth_of] || 1 Dir[File.join(path, * ["*"] * depth + [""])] end &gt;&gt; subdirectories_of("/var") =&gt; ["/var/agentx/", "/var/amavis/", "/var/at/", "/var/audit/", "/var/backups/", "/var/db/", "/var/empty/", "/var/folders/", ...] &gt;&gt; subdirectories_of("/var", :at_depth_of =&gt; 2) =&gt; ["/var/amavis/db/", "/var/amavis/tmp/", "/var/at/jobs/", "/var/at/spool/", "/var/at/tabs/", "/var/at/tmp/", &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 06 Feb 2008 19:25:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5111</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Ruby: Open a file, write to it, and close it in one line</title>
      <link>http://snippets.dzone.com/posts/show/5051</link>
      <description>r - Open a file for reading. The file must exist.&lt;br /&gt;w - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.&lt;br /&gt;a - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.&lt;br /&gt;r+ - Open a file for update both reading and writing. The file must exist.&lt;br /&gt;w+ - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.&lt;br /&gt;a+ - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;File.open(local_filename, 'w') {|f| f.write(doc) }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 29 Jan 2008 18:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5051</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Java filecopy using NIO</title>
      <link>http://snippets.dzone.com/posts/show/4946</link>
      <description>&lt;code&gt;&lt;br /&gt;    public static void fileCopy( File in, File out )&lt;br /&gt;            throws IOException&lt;br /&gt;    {&lt;br /&gt;        FileChannel inChannel = new FileInputStream( in ).getChannel();&lt;br /&gt;        FileChannel outChannel = new FileOutputStream( out ).getChannel();&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;//          inChannel.transferTo(0, inChannel.size(), outChannel);      // original -- apparently has trouble copying large files on Windows&lt;br /&gt;&lt;br /&gt;            // magic number for Windows, 64Mb - 32Kb)&lt;br /&gt;            int maxCount = (64 * 1024 * 1024) - (32 * 1024);&lt;br /&gt;            long size = inChannel.size();&lt;br /&gt;            long position = 0;&lt;br /&gt;            while ( position &lt; size )&lt;br /&gt;            {&lt;br /&gt;               position += inChannel.transferTo( position, maxCount, outChannel );&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        finally&lt;br /&gt;        {&lt;br /&gt;            if ( inChannel != null )&lt;br /&gt;            {&lt;br /&gt;               inChannel.close();&lt;br /&gt;            }&lt;br /&gt;            if ( outChannel != null )&lt;br /&gt;            {&lt;br /&gt;                outChannel.close();&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 04 Jan 2008 22:40:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4946</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
  </channel>
</rss>
