1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19
20 package org.apache.rat.walker;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
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 * Walks various kinds of archives files
42 */
43 public class ArchiveWalker extends Walker implements IReportable {
44
45 /**
46 * Constructs a walker.
47 * @param file not null
48 * @param filter filters input files (optional),
49 * or null when no filtering should be performed
50 * @throws FileNotFoundException in case of I/O errors.
51 */
52 public ArchiveWalker(File file, final FilenameFilter filter) throws FileNotFoundException {
53 super(file, filter);
54 }
55
56 /**
57 * Run a report over all files and directories in this GZIPWalker,
58 * ignoring any files/directories set to be ignored.
59 *
60 * @param report the defined RatReport to run on this GZIP walker.
61 *
62 */
63 public void run(final RatReport report) throws RatException {
64
65 try {
66 ArchiveInputStream input;
67
68 /* I am really sad that classes aren't first-class objects in
69 Java :'( */
70 try {
71 input = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
72 } catch (IOException e) {
73 try {
74 input = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(file)));
75 } catch (IOException e2) {
76 input = new ZipArchiveInputStream(new FileInputStream(file));
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() && !ignored(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 * Report on the given file.
108 *
109 * @param report the report to process the file with
110 * @param file the file to be reported on
111 * @throws RatException
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 }