-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path126.java
More file actions
79 lines (70 loc) · 2.88 KB
/
Copy path126.java
File metadata and controls
79 lines (70 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.lazerycode.selenium.extract;
import com.lazerycode.selenium.exceptions.InvalidFileTypeException;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import static com.lazerycode.selenium.extract.DownloadableFileType.BZ2;
import static com.lazerycode.selenium.extract.DownloadableFileType.GZ;
class CompressedFile {
private static final Logger LOG = Logger.getLogger(FileExtractor.class);
private File compressedFile;
private String decompressedFilename;
private DownloadableFileType filetype = null;
/**
* A class that will take a compressed file.
*
* @param compressedFile A compressed file of type .gz or .bz2
* @throws InvalidFileTypeException InvalidFileTypeException
*/
CompressedFile(File compressedFile) throws InvalidFileTypeException {
filetype = DownloadableFileType.valueOf(FilenameUtils.getExtension(compressedFile.getName()).toUpperCase());
if (filetype != GZ && filetype != BZ2) {
throw new InvalidFileTypeException(compressedFile.getName() + " is an archive, not a known compressed file type");
}
this.compressedFile = compressedFile;
decompressedFilename = FilenameUtils.getBaseName(compressedFile.getName());
}
/**
* Get the filename of the decompressed file.
*
* @return The decompressed filename as a String.
*/
String getDecompressedFilename() {
return decompressedFilename;
}
/**
* Get an InputStream for the decompressed file.
*
* @return An InputStream, the type could be BZip2CompressorInputStream, or GzipCompressorInputStream depending on what type of file was initially supplied
* @throws IOException IOException
*/
InputStream getInputStream() throws IOException {
switch (filetype) {
case GZ:
LOG.debug("Decompressing .gz file");
return new GzipCompressorInputStream(new FileInputStream(compressedFile));
case BZ2:
LOG.debug("Decompressing .bz2 file");
return new BZip2CompressorInputStream(new FileInputStream(compressedFile));
}
return null;
}
/**
* Find out if the uncompressed file is an archive
*
* @return ArchiveType.TAR if a tar, or null if not an archive
*/
DownloadableFileType getArchiveType() {
try {
return DownloadableFileType.valueOf(FilenameUtils.getExtension(decompressedFilename).toUpperCase());
} catch (IllegalArgumentException e) {
LOG.debug("Not a recognised Archive type");
return null;
}
}
}