How to deploy a tar file using Apache Commons

I use the Apache Commons 1.4.1 library to compress and decompress the ".tar.gz" files.

I am having problems with the last bit - converting TarArchiveInputStream to FileOutputStream .

Oddly enough, it breaks in this line:

 FileOutputStream fout = new FileOutputStream(destPath); 



destPath is a canonical path file: C: \ Documents and Settings \ Administrator \ My Documents \ JavaWorkspace \ BackupUtility \ untarred \ Test \ subdir \ testinsub.txt

Mistake:

 Exception in thread "main" java.io.IOException: The system cannot find the path specified 

Any idea what this could be? And why can't he find a way?

I am attaching the whole method below (most of which is taken from here ).

 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(); } 
+6
java apache-commons file-io tar decompression
Jul 11 2018-12-12T00:
source share
2 answers

A few general points, why are you doing voodoo with the File constructor, where there is an excellent constructor used , where you can define the name of the File you want to create and provide the parent file?

Secondly, I'm not too sure how empty spaces are handled in windows paths. This may be the cause of your problems. Try using the constructor I mentioned above and see if it matters: File destPath = new File(dest, tarEntry.getName()); (It is assumed that File dest is the correct file and exists and is accessible to you.

Third, before you do anything with the File object, you must check if it exists and is available. This will ultimately help you identify the problem.

+5
Jul 11 2018-12-11T00:
source share

There is a java heap error in your program. so I think we need to change a bit. here is the code ...

 public static void uncompressTarGZ(File tarFile, File dest) throws IOException { dest.mkdir(); TarArchiveInputStream tarIn = null; tarIn = new TarArchiveInputStream( new GzipCompressorInputStream( new BufferedInputStream( new FileInputStream( tarFile ) ) ) ); 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, tarEntry.getName()); System.out.println("working: " + destPath.getCanonicalPath()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); //byte [] btoRead = new byte[(int)tarEntry.getSize()]; byte [] btoRead = new byte[1024]; //FileInputStream fin // = new FileInputStream(destPath.getCanonicalPath()); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len = 0; while((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead,0,len); } bout.close(); btoRead = null; } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); } 

luck

+13
Jan 08 '13 at 9:13
source share



All Articles