Package org.apache.tools.tar

Examples of org.apache.tools.tar.TarInputStream


  private static File uncompress(File compressedFile, File targetDirectory) {
    File targetDir = null;
    if( compressedFile.getName().endsWith(".tar.gz") ) {
      try {
        TarInputStream in = new TarInputStream(new GZIPInputStream(new FileInputStream(compressedFile)));
        TarEntry e;
        while( (e = in.getNextEntry()) != null ) {
          if( e.isDirectory() ) {
            File f = new File(targetDirectory,e.getName());
            f.mkdirs();
            if( targetDir == null ) {
              targetDir = f;
            }
          } else {
            File f = new File(targetDirectory,e.getName());
            if( ! f.getParentFile().exists() ) {
              f.getParentFile().mkdirs();
            }
            in.copyEntryContents(new FileOutputStream(f));
           
            int m = e.getMode();
            if( (m & OWNER_EXEC) == OWNER_EXEC
                || (m & GROUP_EXEC) == GROUP_EXEC
                || (m & OTHER_EXEC) == OTHER_EXEC ) {
              f.setExecutable(true, false);
            } else if( e.getLinkName() != null && e.getLinkName().trim().length() > 0 ) {
              //TODO Handle symlinks
//              System.err.println("A LINK: " + e.getLinkName());
            }
          }
        }
        in.close();
       
        if( targetDir == null ) {
          targetDir = new File(targetDirectory,"eclipse");
        }
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    } else if( compressedFile.getName().endsWith(".zip") ) {
      try {
        ZipInputStream in = new ZipInputStream(new FileInputStream(compressedFile));
        ZipEntry e;
        while( (e = in.getNextEntry()) != null ) {
          if( e.isDirectory() ) {
            File f = new File(targetDirectory,e.getName());
            f.mkdirs();
            if( targetDir == null ) {
              targetDir = f;
            }
          } else {
            File f = new File(targetDirectory,e.getName());
            if( ! f.getParentFile().exists() ) {
              f.getParentFile().mkdirs();
            }
            FileOutputStream out = new FileOutputStream(f);
            byte[] buf = new byte[1024];
            int l;
            while( (l = in.read(buf, 0, 1024)) != -1 ) {
              out.write(buf,0,l);
            }
            out.close();
          }
          in.closeEntry();
        }
        in.close();
       
        if( targetDir == null ) {
          targetDir = new File(targetDirectory,"eclipse");
        }
      } catch (FileNotFoundException e) {
View Full Code Here


    public void setCompression(UntarCompressionMethod method) {
        compression = method;
    }

    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
        TarInputStream tis = null;
        try {
            log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
            tis = new TarInputStream(
                compression.decompress(srcF,
                    new BufferedInputStream(
                        new FileInputStream(srcF))));
            TarEntry te = null;

            while ((te = tis.getNextEntry()) != null) {
                extractFile(fileUtils, srcF, dir, tis,
                            te.getName(), te.getModTime(), te.isDirectory());
            }
            log("expand complete", Project.MSG_VERBOSE);

        } catch (IOException ioe) {
            throw new BuildException("Error while expanding " + srcF.getPath(),
                                     ioe, location);
        } finally {
            if (tis != null) {
                try {
                    tis.close();
                } catch (IOException e) {}
            }
        }
    }
View Full Code Here

                                 + " task doesn't support the encoding"
                                 + " attribute", getLocation());
    }

    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
        TarInputStream tis = null;
        try {
            log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
            tis = new TarInputStream(
                compression.decompress(srcF,
                    new BufferedInputStream(
                        new FileInputStream(srcF))));
            TarEntry te = null;

            while ((te = tis.getNextEntry()) != null) {
                extractFile(fileUtils, srcF, dir, tis,
                            te.getName(), te.getModTime(), te.isDirectory());
            }
            log("expand complete", Project.MSG_VERBOSE);

        } catch (IOException ioe) {
            throw new BuildException("Error while expanding " + srcF.getPath(),
                                     ioe, getLocation());
        } finally {
            if (tis != null) {
                try {
                    tis.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
View Full Code Here

   * @throws Exception (IOException or FileNotFoundException)
   */
  public static void unTar(final InputStream in, final String untarDir) throws Exception{
    Log.logInfo("UNTAR", "starting");
    if(new File(untarDir).exists()){
      final TarInputStream tin = new TarInputStream(in);
      TarEntry tarEntry = tin.getNextEntry();
      while(tarEntry != null){
        final File destPath = new File(untarDir + File.separator + tarEntry.getName());
        if (!tarEntry.isDirectory()) {
          new File(destPath.getParent()).mkdirs(); // create missing subdirectories
          final FileOutputStream fout = new FileOutputStream(destPath);
          tin.copyEntryContents(fout);
          fout.close();
        } else {
          destPath.mkdir();
        }
        tarEntry = tin.getNextEntry();
      }
      tin.close();
    } else { // untarDir doesn't exist
      Log.logWarning("UNTAR", "destination " + untarDir + " doesn't exist.");
    }
    Log.logInfo("UNTAR", "finished");
  }
View Full Code Here

            } catch (final IOException e) {
                throw new Parser.Failure("tar parser: " + e.getMessage(), url);
            }
        }
        TarEntry entry;
        final TarInputStream tis = new TarInputStream(source);
        File tmp = null;

        // loop through the elements in the tar file and parse every single file inside
        while (true) {
            try {
                if (tis.available() <= 0) break;
                entry = tis.getNextEntry();
                if (entry == null) break;
                if (entry.isDirectory() || entry.getSize() <= 0) continue;
                final String name = entry.getName();
                final int idx = name.lastIndexOf('.');
                final String mime = TextParser.mimeOf((idx > -1) ? name.substring(idx+1) : "");
View Full Code Here

     * @param path
     * @param fileName
     */
    private void untar ( InputStream bundle, String path, String fileName ) {
        TarEntry entry;
        TarInputStream inputStream = null;
        FileOutputStream outputStream = null;

        try {
          //Clean the bundler folder if exist to clean dirty data
          String previousFolderPath = path.replace(fileName, "");
          File previousFolder = new File(previousFolderPath);
          if(previousFolder.exists()){
            FileUtils.cleanDirectory(previousFolder);
          }
            // get a stream to tar file
            InputStream gstream = new GZIPInputStream( bundle );
            inputStream = new TarInputStream( gstream );

            // For each entry in the tar, extract and save the entry to the file
            // system
            while ( null != (entry = inputStream.getNextEntry()) ) {
                // for each entry to be extracted
                int bytesRead;

                String pathWithoutName = path.substring( 0,
                        path.indexOf( fileName ) );

                // if the entry is a directory, create the directory
                if ( entry.isDirectory() ) {
                    File fileOrDir = new File( pathWithoutName + entry.getName() );
                    fileOrDir.mkdir();
                    continue;
                }

                // write to file
                byte[] buf = new byte[1024];
                outputStream = new FileOutputStream( pathWithoutName
                        + entry.getName() );
                while ( (bytesRead = inputStream.read( buf, 0, 1024 )) > -1 )
                    outputStream.write( buf, 0, bytesRead );
                try {
                    if ( null != outputStream ) {
                        outputStream.close();
                    }
                } catch ( Exception e ) {
                    Logger.warn( this.getClass(), "Error Closing Stream.", e );
                }
            }// while

        } catch ( Exception e ) {
            e.printStackTrace();
        } finally { // close your streams
            if ( inputStream != null ) {
                try {
                    inputStream.close();
                } catch ( IOException e ) {
                    Logger.warn( this.getClass(), "Error Closing Stream.", e );
                }
            }
            if ( outputStream != null ) {
View Full Code Here

    @Override
    public void analyze(Document doc, InputStream in) throws IOException {
        content.setLength(0);

        TarInputStream zis = new TarInputStream(in);
        TarEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            content.append(entry.getName()).append('\n');
        }
        content.trimToSize();
        doc.add(new Field("full", dummy));
    }
View Full Code Here

                    found.set(true);

                    assertEquals("header 0", (byte) 'B', content[0]);
                    assertEquals("header 1", (byte) 'Z', content[1]);

                    TarInputStream tar = new TarInputStream(new BZip2CompressorInputStream(new ByteArrayInputStream(content)));
                    while ((tar.getNextEntry()) != null) ;
                    tar.close();
                }
            }
        });
       
        assertTrue("bz2 file not found", found.get());
View Full Code Here

                    assertEquals("header 2", (byte) 'z', content[2]);
                    assertEquals("header 3", (byte) 'X', content[3]);
                    assertEquals("header 4", (byte) 'Z', content[4]);
                    assertEquals("header 5", (byte) '\0', content[5]);
                   
                    TarInputStream tar = new TarInputStream(new XZCompressorInputStream(new ByteArrayInputStream(content)));
                    while ((tar.getNextEntry()) != null) ;
                    tar.close();
                }
            }
        });
       
        assertTrue("xz file not found", found.get());
View Full Code Here

TOP

Related Classes of org.apache.tools.tar.TarInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.