Skip to content

Commit fb0a9c3

Browse files
committed
Support for renaming; filtering unarchive process
1 parent c54aa2c commit fb0a9c3

File tree

13 files changed

+516
-75
lines changed

13 files changed

+516
-75
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fizzed.blaze;
2+
3+
import com.fizzed.blaze.archive.Unarchive;
4+
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
9+
public class Archives {
10+
11+
static public Unarchive unarchive(Path file) {
12+
return new Unarchive(Contexts.currentContext(), file);
13+
}
14+
15+
static public Unarchive unarchive(File file) {
16+
return new Unarchive(Contexts.currentContext(), file.toPath());
17+
}
18+
19+
static public Unarchive unarchive(String file) {
20+
return new Unarchive(Contexts.currentContext(), Paths.get(file));
21+
}
22+
23+
}

blaze-archive/src/main/java/com/fizzed/blaze/archive/ArchiveFormat.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
public class ArchiveFormat {
44

5-
final private String compressMethod;
6-
final private String archiveMethod;
5+
final private Archiver archiver;
6+
final private Compressor compressor;
77
final private String[] extensions;
88

9-
public ArchiveFormat(String archiveMethod, String compressMethod, String... extensions) {
10-
this.compressMethod = compressMethod;
11-
this.archiveMethod = archiveMethod;
9+
public ArchiveFormat(Archiver archiver, Compressor compressor, String... extensions) {
10+
this.archiver = archiver;
11+
this.compressor = compressor;
1212
this.extensions = extensions;
1313
}
1414

15-
public String getCompressMethod() {
16-
return compressMethod;
15+
public Archiver getArchiver() {
16+
return archiver;
1717
}
1818

19-
public String getArchiveMethod() {
20-
return archiveMethod;
19+
public Compressor getCompressor() {
20+
return compressor;
2121
}
2222

2323
public String[] getExtensions() {
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
package com.fizzed.blaze.archive;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

5-
import static java.util.Arrays.asList;
6-
76
public class ArchiveFormats {
87

9-
static public final List<ArchiveFormat> ALL = asList(
10-
new ArchiveFormat("zip", null, ".zip"),
11-
new ArchiveFormat("tar", null, ".tar"),
12-
new ArchiveFormat("tar", "gz", ".tar.gz", ".tgz"),
13-
new ArchiveFormat("tar", "bzip2", ".tar.bz2"),
14-
new ArchiveFormat("tar", "xz", ".tar.xz"),
15-
new ArchiveFormat("tar", "zstd", ".tar.zst"),
16-
new ArchiveFormat(null, "gz", ".gz"),
17-
new ArchiveFormat("7z", null, ".7z")
18-
);
8+
static public final List<ArchiveFormat> ALL;
9+
static {
10+
ALL = new ArrayList<>();
11+
// build our compatability list, then add in any special extra cases (e.g. extensions)
12+
// add all the archives in first
13+
for (Archiver archiver : Archiver.values()) {
14+
ALL.add(new ArchiveFormat(archiver, null, archiver.getExtension()));
15+
}
16+
17+
// .tar archives can simply be compressed with any compression we support
18+
for (Compressor compressor : Compressor.values()) {
19+
ALL.add(new ArchiveFormat(Archiver.TAR, compressor, Archiver.TAR.getExtension() + compressor.getExtension()));
20+
}
21+
22+
// .tgz special case
23+
ALL.add(new ArchiveFormat(Archiver.TAR, Compressor.GZ, ".tgz"));
1924

25+
// all compressors too
26+
for (Compressor compressor : Compressor.values()) {
27+
ALL.add(new ArchiveFormat(null, compressor, compressor.getExtension()));
28+
}
29+
}
30+
31+
/*
2032
static public ArchiveFormat detectByFileName(String fileName) {
2133
final String n = fileName.toLowerCase();
2234
@@ -34,6 +46,6 @@ static public ArchiveFormat detectByFileName(String fileName) {
3446
}
3547
3648
return matchedFormat;
37-
}
49+
}*/
3850

3951
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.fizzed.blaze.archive;
2+
3+
import java.nio.file.Path;
4+
5+
public class ArchiveHelper {
6+
7+
static public ArchiveInfo archiveInfo(Path file) {
8+
return archiveInfo(file.getFileName().toString());
9+
}
10+
11+
static public ArchiveInfo archiveInfo(String archivedName) {
12+
final String n = archivedName.toLowerCase();
13+
14+
// find the longest matching extension
15+
String matchedExtension = null;
16+
ArchiveFormat matchedFormat = null;
17+
18+
for (ArchiveFormat af : ArchiveFormats.ALL) {
19+
for (String ext : af.getExtensions()) {
20+
if (n.endsWith(ext) && (matchedExtension == null || ext.length() > matchedExtension.length())) {
21+
matchedExtension = ext;
22+
matchedFormat = af;
23+
}
24+
}
25+
}
26+
27+
if (matchedExtension == null) {
28+
return null;
29+
}
30+
31+
// build out the info we need
32+
String unarchivedName = archivedName.substring(0, archivedName.length()-matchedExtension.length());
33+
34+
return new ArchiveInfo(matchedFormat.getArchiver(), matchedFormat.getCompressor(), archivedName, unarchivedName);
35+
}
36+
37+
/**
38+
* Strips a path from the front of an entry name, flattening it. For example, an entry name of sample/a/b.txt
39+
* would become a/b.txt if componentCount=1 and b.txt if componentCount=2 or 3, etc.
40+
* @param entryName
41+
* @param componentCount
42+
* @return
43+
*/
44+
static public String[] stripComponents(String entryName, int componentCount) {
45+
final String[] result = new String[2];
46+
47+
int stripPos = -1;
48+
if (componentCount > 0) {
49+
for (int i = 0; i < componentCount; i++) {
50+
int slashPos = entryName.indexOf('/', stripPos);
51+
if (slashPos <= stripPos) {
52+
break; // done finding components
53+
}
54+
stripPos = slashPos + 1;
55+
}
56+
}
57+
58+
if (stripPos < 0) {
59+
result[0] = entryName;
60+
result[1] = null;
61+
} else {
62+
result[0] = entryName.substring(stripPos);
63+
result[1] = entryName.substring(0, stripPos);
64+
}
65+
66+
return result;
67+
}
68+
69+
static public String getCommonsCompressorName(Compressor compressor) {
70+
switch (compressor) {
71+
case GZ:
72+
return "gz";
73+
case BZ2:
74+
return "bzip2";
75+
case XZ:
76+
return "xz";
77+
case ZSTD:
78+
return "zstd";
79+
default:
80+
throw new IllegalArgumentException("Unable to map compressor " + compressor + " to an apache commons compressor name (was it not added?)");
81+
}
82+
}
83+
84+
static public String getCommonsArchiverName(Archiver archiver) {
85+
switch (archiver) {
86+
case TAR:
87+
return "tar";
88+
case ZIP:
89+
return "zip";
90+
case SEVENZ:
91+
return "7z";
92+
default:
93+
throw new IllegalArgumentException("Unable to map archiver " + archiver + " to an apache commons archiver name (was it not added?)");
94+
}
95+
}
96+
97+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fizzed.blaze.archive;
2+
3+
public class ArchiveInfo {
4+
5+
private Archiver archiver;
6+
private Compressor compressor;
7+
private String archivedName;
8+
private String unarchivedName;
9+
10+
public ArchiveInfo(Archiver archiver, Compressor compressor, String archivedName, String unarchivedName) {
11+
this.archiver = archiver;
12+
this.compressor = compressor;
13+
this.archivedName = archivedName;
14+
this.unarchivedName = unarchivedName;
15+
}
16+
17+
public Archiver getArchiver() {
18+
return archiver;
19+
}
20+
21+
public ArchiveInfo setArchiver(Archiver archiver) {
22+
this.archiver = archiver;
23+
return this;
24+
}
25+
26+
public Compressor getCompressor() {
27+
return compressor;
28+
}
29+
30+
public ArchiveInfo setCompressor(Compressor compressor) {
31+
this.compressor = compressor;
32+
return this;
33+
}
34+
35+
public String getArchivedName() {
36+
return archivedName;
37+
}
38+
39+
public ArchiveInfo setArchivedName(String archivedName) {
40+
this.archivedName = archivedName;
41+
return this;
42+
}
43+
44+
public String getUnarchivedName() {
45+
return unarchivedName;
46+
}
47+
48+
public ArchiveInfo setUnarchivedName(String unarchivedName) {
49+
this.unarchivedName = unarchivedName;
50+
return this;
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.fizzed.blaze.archive;
2+
3+
public enum Archiver {
4+
5+
ZIP(".zip"),
6+
TAR(".tar"),
7+
SEVENZ(".7z");
8+
9+
private final String extension;
10+
11+
Archiver(String extension) {
12+
this.extension = extension;
13+
}
14+
15+
public String getExtension() {
16+
return extension;
17+
}
18+
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.fizzed.blaze.archive;
2+
3+
public enum Compressor {
4+
5+
GZ(".gz"),
6+
BZ2(".bz2"),
7+
XZ(".xz"),
8+
ZSTD(".zst");
9+
10+
private final String extension;
11+
12+
Compressor(String extension) {
13+
this.extension = extension;
14+
}
15+
16+
public String getExtension() {
17+
return extension;
18+
}
19+
20+
}

0 commit comments

Comments
 (0)