View Javadoc

1   /*
2    * Copyright (C) 2013 Room Work eXperience
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package net.rwx.maven.asciidoc.utils;
18  
19  import java.io.*;
20  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
21  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
22  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
23  
24  /**
25   *
26   * @author Arnaud Fonce <arnaud.fonce@r-w-x.net>
27   */
28  public class FileUtils extends org.apache.commons.io.FileUtils {
29  
30      private static File temporaryDirectory;
31      
32      public static String getAsciidocTemporaryPath( String filename, String extension ) throws IOException {
33      
34          File file = new File( filename );
35          
36          StringBuilder builder = new StringBuilder();
37          
38          builder.append( getTemporayAsciidoc() );
39          builder.append( File.separator );
40          builder.append( file.getName() );
41          builder.append( extension );
42          
43          return builder.toString();
44      }
45      
46      public static void moveFileToDirectory( String fileName, String directoryName ) throws IOException {
47          
48          File file = new File( fileName );
49          File directory = new File( directoryName );
50          
51          File destFile = FileUtils.getFile( directory, file.getName() );
52          if ( destFile.exists() ) {
53              forceDelete( destFile );
54          }
55          
56          moveFileToDirectory( file, directory, true );
57      }
58      
59      public static String getTemporaryDirectory() throws IOException {
60          StringBuilder builder = new StringBuilder();
61          
62          builder.append( getTempDirectoryPath() );
63          builder.append( File.separator );
64          builder.append( "asciidoc-maven-plugin" );
65          builder.append( Long.toString(System.nanoTime()) );
66          
67          File directory = new File( builder.toString() );
68          forceMkdir( directory );
69          // forceDeleteOnExit( directory );
70          
71          return directory.getAbsolutePath();
72      }
73      public static String getTemporayAsciidoc( ) throws IOException {
74  
75          if( temporaryDirectory == null )
76          {
77              StringBuilder builder = new StringBuilder();
78          
79              builder.append( getTempDirectoryPath() );
80              builder.append( File.separator );
81              builder.append( "asciidoc-maven-plugin" );
82              builder.append( Long.toString(System.nanoTime()) );
83              temporaryDirectory = new File( builder.toString() );
84              forceMkdir( temporaryDirectory );
85              forceDeleteOnExit( temporaryDirectory );
86          }
87          // File f = File.createTempFile("tempAsciidoc", Long.toString(System.nanoTime()));
88          // forceMkdir( f );
89          // forceDeleteOnExit( f );
90          return temporaryDirectory.getAbsolutePath();
91  
92      }
93      
94      public static String uncompress( InputStream is, String destination ) throws IOException {
95          
96          BufferedInputStream in = new BufferedInputStream( is );
97          GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
98          TarArchiveInputStream tarInput = new TarArchiveInputStream( gzIn );
99          
100         TarArchiveEntry entry = tarInput.getNextTarEntry();
101         do {
102             File f = new File( destination + "/" + entry.getName() );
103             FileUtils.forceMkdir( f.getParentFile() );
104             
105             if( ! f.isDirectory() ) {
106                 OutputStream os = new FileOutputStream( f );
107                 byte[] content = new byte[ (int)entry.getSize() ];
108                 int byteRead = 0;
109                 while( byteRead < entry.getSize() ) {
110                     byteRead += tarInput.read(content, byteRead, content.length - byteRead);
111                     os.write( content, 0, byteRead );
112                 }
113 
114                 os.close();
115                 forceDeleteOnExit( f );
116             }
117             entry = tarInput.getNextTarEntry();
118         }while( entry != null );
119 
120         gzIn.close();
121         
122         return destination;
123     }
124 }