1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.rat.walker;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.nio.file.Files;
27
28 import org.apache.commons.compress.archivers.ArchiveEntry;
29 import org.apache.commons.compress.archivers.ArchiveInputStream;
30 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
31 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
32 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
33 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
34 import org.apache.rat.api.Document;
35 import org.apache.rat.api.RatException;
36 import org.apache.rat.document.impl.ArchiveEntryDocument;
37 import org.apache.rat.report.IReportable;
38 import org.apache.rat.report.RatReport;
39
40
41
42
43 public class ArchiveWalker extends Walker implements IReportable {
44
45
46
47
48
49
50
51
52 public ArchiveWalker(File file, final FilenameFilter filter) throws FileNotFoundException {
53 super(file, filter);
54 }
55
56
57
58
59
60
61
62
63 public void run(final RatReport report) throws RatException {
64
65 try {
66 ArchiveInputStream<? extends ArchiveEntry> input;
67
68
69
70 try {
71 input = new TarArchiveInputStream(new GzipCompressorInputStream(Files.newInputStream(file.toPath())));
72 } catch (IOException e) {
73 try {
74 input = new TarArchiveInputStream(new BZip2CompressorInputStream(Files.newInputStream(file.toPath())));
75 } catch (IOException e2) {
76 input = new ZipArchiveInputStream(Files.newInputStream(file.toPath()));
77 }
78 }
79
80 ArchiveEntry entry = input.getNextEntry();
81 while (entry != null) {
82 File f = new File(entry.getName());
83 byte[] contents = new byte[(int) entry.getSize()];
84 int offset = 0;
85 int length = contents.length;
86
87 while (offset < entry.getSize()) {
88 int actualRead = input.read(contents, offset, length);
89 length -= actualRead;
90 offset += actualRead;
91 }
92
93 if (!entry.isDirectory() && isNotIgnored(f)) {
94 report(report, contents, f);
95 }
96
97 entry = input.getNextEntry();
98 }
99
100 input.close();
101 } catch (IOException e) {
102 throw new RatException(e);
103 }
104 }
105
106
107
108
109
110
111
112
113 private void report(final RatReport report, byte[] contents, File file) throws RatException {
114
115 Document document = new ArchiveEntryDocument(file, contents);
116 report.report(document);
117
118 }
119 }