added GZ support
This commit is contained in:
parent
b65f459511
commit
5f6b44fb4a
@ -859,13 +859,14 @@ public class FileOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".tar")) {
|
} else if (name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".tar") || name.endsWith(".gz")) {
|
||||||
InputStream is = new FileInputStream(archive);
|
InputStream is = new FileInputStream(archive);
|
||||||
if (name.endsWith(".gz") || name.endsWith(".tgz")) {
|
if (name.endsWith(".gz") || name.endsWith(".tgz")) {
|
||||||
is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(is);
|
is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(is);
|
||||||
}
|
}
|
||||||
try (org.apache.commons.compress.archivers.tar.TarArchiveInputStream tais = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(is)) {
|
try (org.apache.commons.compress.archivers.tar.TarArchiveInputStream tais = new org.apache.commons.compress.archivers.tar.TarArchiveInputStream(is)) {
|
||||||
org.apache.commons.compress.archivers.tar.TarArchiveEntry entry;
|
org.apache.commons.compress.archivers.tar.TarArchiveEntry entry;
|
||||||
|
boolean found = false;
|
||||||
while ((entry = tais.getNextTarEntry()) != null) {
|
while ((entry = tais.getNextTarEntry()) != null) {
|
||||||
if (entry.getName().equals(entryPath) || (archive.getAbsolutePath() + File.separator + entry.getName()).equals(entryPath)) {
|
if (entry.getName().equals(entryPath) || (archive.getAbsolutePath() + File.separator + entry.getName()).equals(entryPath)) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
@ -877,6 +878,23 @@ public class FileOperations {
|
|||||||
return baos.toByteArray();
|
return baos.toByteArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!found && name.endsWith(".gz") && !name.endsWith(".tar.gz")) {
|
||||||
|
// If it's just a .gz file, it might contain a single file with the decompressed name
|
||||||
|
String originalName = archive.getName();
|
||||||
|
String targetName = originalName.substring(0, originalName.length() - 3);
|
||||||
|
if (targetName.equals(entryPath) || (archive.getAbsolutePath() + File.separator + targetName).equals(entryPath)) {
|
||||||
|
// Re-read as simple gzip
|
||||||
|
try (InputStream simpleGzip = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(new FileInputStream(archive))) {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[65536];
|
||||||
|
int len;
|
||||||
|
while ((len = simpleGzip.read(buffer)) > 0) {
|
||||||
|
baos.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
return baos.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (name.endsWith(".7z")) {
|
} else if (name.endsWith(".7z")) {
|
||||||
SevenZFile szf;
|
SevenZFile szf;
|
||||||
@ -926,7 +944,7 @@ public class FileOperations {
|
|||||||
public static boolean isArchiveFile(String filename) {
|
public static boolean isArchiveFile(String filename) {
|
||||||
if (filename == null) return false;
|
if (filename == null) return false;
|
||||||
String n = filename.toLowerCase();
|
String n = filename.toLowerCase();
|
||||||
return n.endsWith(".car") || n.endsWith(".war") || n.endsWith(".zip") || n.endsWith(".jar") || n.endsWith(".tar") || n.endsWith(".tar.gz") || n.endsWith(".tgz") || n.endsWith(".7z") || n.endsWith(".rar");
|
return n.endsWith(".car") || n.endsWith(".war") || n.endsWith(".zip") || n.endsWith(".jar") || n.endsWith(".tar") || n.endsWith(".tar.gz") || n.endsWith(".tgz") || n.endsWith(".gz") || n.endsWith(".7z") || n.endsWith(".rar");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1023,6 +1041,15 @@ public class FileOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try GZIP
|
||||||
|
if (name.endsWith(".gz") || name.endsWith(".tgz")) {
|
||||||
|
try (InputStream is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(new FileInputStream(file))) {
|
||||||
|
return true;
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
// Not a valid gzip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try TAR/TAR.GZ
|
// Try TAR/TAR.GZ
|
||||||
if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
|
if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
|
||||||
try {
|
try {
|
||||||
@ -1084,7 +1111,7 @@ public class FileOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".tar")) {
|
} else if (name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".tar") || name.endsWith(".gz")) {
|
||||||
InputStream is = new FileInputStream(archive);
|
InputStream is = new FileInputStream(archive);
|
||||||
if (name.endsWith(".gz") || name.endsWith(".tgz")) {
|
if (name.endsWith(".gz") || name.endsWith(".tgz")) {
|
||||||
is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(is);
|
is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(is);
|
||||||
@ -1107,6 +1134,28 @@ public class FileOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Fallback for single .gz file if no TAR entries found
|
||||||
|
if (name.endsWith(".gz") && !name.endsWith(".tar.gz")) {
|
||||||
|
String targetName = archive.getName().substring(0, archive.getName().length() - 3);
|
||||||
|
boolean nameMatched = true;
|
||||||
|
if (patternLower != null && !patternLower.isEmpty()) {
|
||||||
|
nameMatched = matchEntry(targetName, patternLower, filenameRegex, caseSensitive);
|
||||||
|
}
|
||||||
|
if (nameMatched) {
|
||||||
|
boolean contentMatched = true;
|
||||||
|
if (contentPattern != null) {
|
||||||
|
// Re-run search on simple gzip stream
|
||||||
|
try (InputStream simpleGzip = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(new FileInputStream(archive))) {
|
||||||
|
contentMatched = searchInStream(simpleGzip, contentPattern);
|
||||||
|
} catch (Exception e) {
|
||||||
|
contentMatched = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (contentMatched) {
|
||||||
|
callback.onFileFound(archive, archive.getAbsolutePath() + File.separator + targetName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (name.endsWith(".7z")) {
|
} else if (name.endsWith(".7z")) {
|
||||||
SevenZFile szf;
|
SevenZFile szf;
|
||||||
@ -1283,8 +1332,17 @@ public class FileOperations {
|
|||||||
extract7z(archive, targetDir, callback);
|
extract7z(archive, targetDir, callback);
|
||||||
} else if (name.endsWith(".rar")) {
|
} else if (name.endsWith(".rar")) {
|
||||||
extractRar(archive, targetDir, callback);
|
extractRar(archive, targetDir, callback);
|
||||||
} else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
|
} else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".gz")) {
|
||||||
extractTar(archive, targetDir, callback);
|
try {
|
||||||
|
extractTar(archive, targetDir, callback);
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (name.endsWith(".gz")) {
|
||||||
|
// Maybe it's just a single gzipped file, not a tarball
|
||||||
|
extractGzip(archive, targetDir, callback);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// For unknown extensions, try to detect format by attempting to read
|
// For unknown extensions, try to detect format by attempting to read
|
||||||
// First try ZIP (most common)
|
// First try ZIP (most common)
|
||||||
@ -1646,6 +1704,27 @@ public class FileOperations {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void extractGzip(File archive, File targetDir, ProgressCallback callback) throws IOException {
|
||||||
|
String name = archive.getName();
|
||||||
|
String targetName = name.toLowerCase().endsWith(".gz") ? name.substring(0, name.length() - 3) : name + ".out";
|
||||||
|
File targetFile = new File(targetDir, targetName);
|
||||||
|
|
||||||
|
try (InputStream is = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archive)));
|
||||||
|
OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) {
|
||||||
|
byte[] buffer = new byte[65536];
|
||||||
|
int len;
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onProgress(0, 0, targetName);
|
||||||
|
}
|
||||||
|
while ((len = is.read(buffer)) > 0) {
|
||||||
|
os.write(buffer, 0, len);
|
||||||
|
if (callback != null) {
|
||||||
|
callback.onProgress(0, 0, targetName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean supportsArchiveRewrite(File archive) {
|
public static boolean supportsArchiveRewrite(File archive) {
|
||||||
if (archive == null) return false;
|
if (archive == null) return false;
|
||||||
String name = archive.getName().toLowerCase();
|
String name = archive.getName().toLowerCase();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user