diff --git a/src/main/java/cz/kamma/kfmanager/service/FileOperations.java b/src/main/java/cz/kamma/kfmanager/service/FileOperations.java index adf2380..48b07b5 100644 --- a/src/main/java/cz/kamma/kfmanager/service/FileOperations.java +++ b/src/main/java/cz/kamma/kfmanager/service/FileOperations.java @@ -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); if (name.endsWith(".gz") || name.endsWith(".tgz")) { 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)) { org.apache.commons.compress.archivers.tar.TarArchiveEntry entry; + boolean found = false; while ((entry = tais.getNextTarEntry()) != null) { if (entry.getName().equals(entryPath) || (archive.getAbsolutePath() + File.separator + entry.getName()).equals(entryPath)) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -877,6 +878,23 @@ public class FileOperations { 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")) { SevenZFile szf; @@ -926,7 +944,7 @@ public class FileOperations { public static boolean isArchiveFile(String filename) { if (filename == null) return false; 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 if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) { 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); if (name.endsWith(".gz") || name.endsWith(".tgz")) { 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")) { SevenZFile szf; @@ -1283,8 +1332,17 @@ public class FileOperations { extract7z(archive, targetDir, callback); } else if (name.endsWith(".rar")) { extractRar(archive, targetDir, callback); - } else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz")) { - extractTar(archive, targetDir, callback); + } else if (name.endsWith(".tar") || name.endsWith(".tar.gz") || name.endsWith(".tgz") || name.endsWith(".gz")) { + 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 { // For unknown extensions, try to detect format by attempting to read // 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) { if (archive == null) return false; String name = archive.getName().toLowerCase();