title: How to untar a TAR file using Apache Commons
body: <p>I'm using the Apache Commons 1.4.1 library to compress and uncompress <code>".tar.gz"</code> files.</p>
  
  <p>I'm having trouble with the last bit -- converting a <code>TarArchiveInputStream</code> into a <code>FileOutputStream</code>.</p>
  
  <p>Oddly enough, it's breaking on this line:</p>
  
  <pre><code>FileOutputStream fout = new FileOutputStream(destPath);
  </code></pre>
  
  <hr>
  
  <p><code>destPath</code> is a File with a Canonical path of: C:\Documents and Settings\Administrator\My Documents\JavaWorkspace\BackupUtility\untarred\Test\subdir\testinsub.txt</p>
  
  <p>Error produced:</p>
  
  <pre><code>Exception in thread "main" java.io.IOException: The system cannot find the path specified
  </code></pre>
  
  <p>Any idea what it could be? And why is it not able to find the path?</p>
  
  <p>I'm attaching the whole method below (most of which is lifted from <a href="http://webcache.googleusercontent.com/search?q=cache%3aOQ1Ex8ChLpkJ%3awww.thetechallies.com/node/4%20&amp;cd=10&amp;hl=en&amp;ct=clnk&amp;gl=mt" rel="nofollow">here</a>).</p>
  
  <pre><code>private void untar(File dest) throws IOException {
      dest.mkdir();
      TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
      // tarIn is a TarArchiveInputStream
      while (tarEntry != null) {// create a file with the same name as the tarEntry
          File destPath = new File(dest.toString() + System.getProperty("file.separator") + tarEntry.getName());
          System.out.println("working: " + destPath.getCanonicalPath());
          if (tarEntry.isDirectory()) {
              destPath.mkdirs();
          } else {
              destPath.createNewFile();
              FileOutputStream fout = new FileOutputStream(destPath);
              tarIn.read(new byte[(int) tarEntry.getSize()]);
              fout.close();
          }
          tarEntry = tarIn.getNextTarEntry();
      }
      tarIn.close();
  }
  </code></pre>