1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
88
89
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 }