Skip to content

Commit

Permalink
compressTar: include directories and their permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Dec 28, 2024
1 parent 30b45c0 commit b9442dd
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions core/src/main/java/lucee/commons/io/compress/CompressUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b9442dd

Please sign in to comment.