From b9442dd8e8542bea4bf1b48daac5560e129c90f1 Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Sat, 28 Dec 2024 17:06:15 +0100 Subject: [PATCH] compressTar: include directories and their permissions --- .../commons/io/compress/CompressUtil.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/lucee/commons/io/compress/CompressUtil.java b/core/src/main/java/lucee/commons/io/compress/CompressUtil.java index e52683a18e..638337a7ce 100644 --- a/core/src/main/java/lucee/commons/io/compress/CompressUtil.java +++ b/core/src/main/java/lucee/commons/io/compress/CompressUtil.java @@ -203,7 +203,7 @@ private static void extractTar(Resource tarFile, Resource targetDir) throws IOEx return; } - // read the zip file and build a query from its contents + // read the tar file and extract its contents TarArchiveInputStream tis = null; try { tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream())); @@ -268,6 +268,7 @@ private static void unzip(Resource zipFile, Resource targetDir) throws IOExcepti try { zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream())); ZipEntry entry; + int mode; while ((entry = zis.getNextEntry()) != null) { Resource target = ZipUtil.toResource(targetDir, entry); if (entry.isDirectory()) { @@ -279,6 +280,10 @@ private static void unzip(Resource zipFile, Resource targetDir) throws IOExcepti if (!target.exists()) IOUtil.copy(zis, target, false); } target.setLastModified(entry.getTime()); + mode = entry.getMode(); + if (mode > 0) { + target.setMode(ModeUtil.extractPermissions(mode, false)); + } zis.closeEntry(); } } @@ -549,29 +554,32 @@ public static void compressTar(String parent, Resource[] sources, TarArchiveOutp } private static void compressTar(String parent, Resource source, TarArchiveOutputStream tos, int mode) throws IOException { - if (source.isFile()) { - // TarEntry entry = (source instanceof FileResource)?new TarEntry((FileResource)source):new - // TarEntry(parent); - TarArchiveEntry entry = new TarArchiveEntry(parent); + String _parent = parent; + if (!source.isFile() && parent.charAt(parent.length() - 1) != '/') + _parent += "/"; // indicates this is a directory - entry.setName(parent); + TarArchiveEntry entry = new TarArchiveEntry(_parent); - // mode - if (mode > 0) entry.setMode(ModeUtil.extractPermissions(mode, false)); - else if ((mode = source.getMode()) > 0) entry.setMode(ModeUtil.extractPermissions(mode, false)); + entry.setName(parent); + // mode + if (mode > 0) entry.setMode(ModeUtil.extractPermissions(mode, false)); + else if ((mode = source.getMode()) > 0) entry.setMode(ModeUtil.extractPermissions(mode, false)); + if (source.isFile()) entry.setSize(source.length()); - entry.setModTime(source.lastModified()); - tos.putArchiveEntry(entry); - try { + entry.setModTime(source.lastModified()); + tos.putArchiveEntry(entry); + try { + if (source.isFile()) IOUtil.copy(source, tos, false); - } - finally { - tos.closeArchiveEntry(); - } } - else if (source.isDirectory()) { - compressTar(parent, source.listResources(), tos, mode); + finally { + tos.closeArchiveEntry(); + } + + if (source.isDirectory()) { + Resource[] sources = source.listResources(); + compressTar(parent, sources, tos, mode); } }