Compare commits

..

No commits in common. "9e02b23f4993a0c5b823ca65b1e08903ed2b8c18" and "1675396d12bdb9b59f485103f713989e170f3f23" have entirely different histories.

View File

@ -567,28 +567,6 @@ public class FileOperations {
} }
} }
private static void setPermissionsFromMode(Path path, int mode) {
if (mode <= 0 || (cz.kamma.kfmanager.MainApp.CURRENT_OS != cz.kamma.kfmanager.MainApp.OS.LINUX &&
cz.kamma.kfmanager.MainApp.CURRENT_OS != cz.kamma.kfmanager.MainApp.OS.MACOS)) {
return;
}
try {
Set<PosixFilePermission> perms = new HashSet<>();
if ((mode & 0400) != 0) perms.add(PosixFilePermission.OWNER_READ);
if ((mode & 0200) != 0) perms.add(PosixFilePermission.OWNER_WRITE);
if ((mode & 0100) != 0) perms.add(PosixFilePermission.OWNER_EXECUTE);
if ((mode & 0040) != 0) perms.add(PosixFilePermission.GROUP_READ);
if ((mode & 0020) != 0) perms.add(PosixFilePermission.GROUP_WRITE);
if ((mode & 0010) != 0) perms.add(PosixFilePermission.GROUP_EXECUTE);
if ((mode & 0004) != 0) perms.add(PosixFilePermission.OTHERS_READ);
if ((mode & 0002) != 0) perms.add(PosixFilePermission.OTHERS_WRITE);
if ((mode & 0001) != 0) perms.add(PosixFilePermission.OTHERS_EXECUTE);
if (!perms.isEmpty()) {
Files.setPosixFilePermissions(path, perms);
}
} catch (Exception ignore) {}
}
/** /**
* Delete directory recursively * Delete directory recursively
*/ */
@ -976,7 +954,7 @@ public class FileOperations {
long totalItems = calculateTotalItems(cleanedItems); long totalItems = calculateTotalItems(cleanedItems);
long[] currentItem = {0}; long[] currentItem = {0};
try (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos = new org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream(targetZipFile)) { try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetZipFile))) {
for (FileItem item : cleanedItems) { for (FileItem item : cleanedItems) {
if (callback != null && callback.isCancelled()) break; if (callback != null && callback.isCancelled()) break;
addToZip(item.getFile(), item.getName(), zos, totalItems, currentItem, callback); addToZip(item.getFile(), item.getName(), zos, totalItems, currentItem, callback);
@ -984,7 +962,7 @@ public class FileOperations {
} }
} }
private static void addToZip(File fileToZip, String fileName, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, long totalItems, long[] currentItem, ProgressCallback callback) throws IOException { private static void addToZip(File fileToZip, String fileName, ZipOutputStream zos, long totalItems, long[] currentItem, ProgressCallback callback) throws IOException {
if (fileToZip.isHidden()) { if (fileToZip.isHidden()) {
return; return;
} }
@ -994,48 +972,32 @@ public class FileOperations {
callback.onProgress(currentItem[0], totalItems, fileName); callback.onProgress(currentItem[0], totalItems, fileName);
} }
org.apache.commons.compress.archivers.zip.ZipArchiveEntry zipEntry = new org.apache.commons.compress.archivers.zip.ZipArchiveEntry(fileToZip, fileName);
// Try to set POSIX permissions
if (cz.kamma.kfmanager.MainApp.CURRENT_OS == cz.kamma.kfmanager.MainApp.OS.LINUX ||
cz.kamma.kfmanager.MainApp.CURRENT_OS == cz.kamma.kfmanager.MainApp.OS.MACOS) {
try {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(fileToZip.toPath());
int mode = 0;
if (perms.contains(PosixFilePermission.OWNER_READ)) mode |= 0400;
if (perms.contains(PosixFilePermission.OWNER_WRITE)) mode |= 0200;
if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) mode |= 0100;
if (perms.contains(PosixFilePermission.GROUP_READ)) mode |= 0040;
if (perms.contains(PosixFilePermission.GROUP_WRITE)) mode |= 0020;
if (perms.contains(PosixFilePermission.GROUP_EXECUTE)) mode |= 0010;
if (perms.contains(PosixFilePermission.OTHERS_READ)) mode |= 0004;
if (perms.contains(PosixFilePermission.OTHERS_WRITE)) mode |= 0002;
if (perms.contains(PosixFilePermission.OTHERS_EXECUTE)) mode |= 0001;
zipEntry.setUnixMode(mode);
} catch (Exception ignore) {}
}
if (fileToZip.isDirectory()) { if (fileToZip.isDirectory()) {
zos.putArchiveEntry(zipEntry); if (fileName.endsWith("/")) {
zos.closeArchiveEntry(); zos.putNextEntry(new ZipEntry(fileName));
zos.closeEntry();
} else {
zos.putNextEntry(new ZipEntry(fileName + "/"));
zos.closeEntry();
}
File[] children = fileToZip.listFiles(); File[] children = fileToZip.listFiles();
if (children != null) { if (children != null) {
for (File childFile : children) { for (File childFile : children) {
addToZip(childFile, fileName + (fileName.endsWith("/") ? "" : "/") + childFile.getName(), zos, totalItems, currentItem, callback); addToZip(childFile, fileName + "/" + childFile.getName(), zos, totalItems, currentItem, callback);
} }
} }
return; return;
} }
try (FileInputStream fis = new FileInputStream(fileToZip)) {
try (InputStream is = Files.newInputStream(fileToZip.toPath())) { ZipEntry zipEntry = new ZipEntry(fileName);
zos.putArchiveEntry(zipEntry); zos.putNextEntry(zipEntry);
long fileSize = fileToZip.length(); long fileSize = fileToZip.length();
long bytesCopied = 0; long bytesCopied = 0;
byte[] buffer = new byte[24576]; byte[] bytes = new byte[24576];
int len; int length;
while ((len = is.read(buffer)) >= 0) { while ((length = fis.read(bytes)) >= 0) {
zos.write(buffer, 0, len); zos.write(bytes, 0, length);
bytesCopied += len; bytesCopied += length;
if (callback != null) { if (callback != null) {
callback.onFileProgress(bytesCopied, fileSize); callback.onFileProgress(bytesCopied, fileSize);
} }
@ -1043,7 +1005,7 @@ public class FileOperations {
if (callback != null) { if (callback != null) {
callback.onFileProgress(fileSize, fileSize); callback.onFileProgress(fileSize, fileSize);
} }
zos.closeArchiveEntry(); zos.closeEntry();
} }
} }
@ -1096,11 +1058,9 @@ public class FileOperations {
File newFile = new File(targetDir, entry.getName()); File newFile = new File(targetDir, entry.getName());
if (entry.isDirectory()) { if (entry.isDirectory()) {
newFile.mkdirs(); newFile.mkdirs();
setPermissionsFromMode(newFile.toPath(), entry.getMode());
} else { } else {
newFile.getParentFile().mkdirs(); newFile.getParentFile().mkdirs();
Files.copy(tais, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(tais, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
setPermissionsFromMode(newFile.toPath(), entry.getMode());
} }
} }
} }
@ -1161,12 +1121,17 @@ public class FileOperations {
Files.createDirectories(targetDirectory.toPath()); Files.createDirectories(targetDirectory.toPath());
} }
try (org.apache.commons.compress.archivers.zip.ZipFile zf = new org.apache.commons.compress.archivers.zip.ZipFile(zipFile)) { long totalItems = 0;
java.util.List<org.apache.commons.compress.archivers.zip.ZipArchiveEntry> entries = java.util.Collections.list(zf.getEntries()); try (ZipFile zf = new ZipFile(zipFile)) {
long totalItems = entries.size(); totalItems = zf.size();
long currentItem = 0; } catch (IOException e) {
// fallback if ZipFile fails
}
for (org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry : entries) { long currentItem = 0;
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
currentItem++; currentItem++;
File newFile = new File(targetDirectory, entry.getName()); File newFile = new File(targetDirectory, entry.getName());
@ -1178,24 +1143,37 @@ public class FileOperations {
if (!newFile.isDirectory() && !newFile.mkdirs()) { if (!newFile.isDirectory() && !newFile.mkdirs()) {
throw new IOException("Failed to create directory " + newFile); throw new IOException("Failed to create directory " + newFile);
} }
setPermissionsFromMode(newFile.toPath(), entry.getUnixMode());
} else { } else {
// create parent directories if they don't exist // create parent directories if they don't exist
File parent = newFile.getParentFile(); File parent = newFile.getParentFile();
if (parent != null && !parent.exists()) { if (!parent.isDirectory() && !parent.mkdirs()) {
parent.mkdirs(); throw new IOException("Failed to create directory " + parent);
} }
if (newFile.exists() && newFile.isDirectory()) { if (newFile.exists() && newFile.isDirectory()) {
deleteDirectoryInternal(newFile.toPath()); deleteDirectoryInternal(newFile.toPath());
} }
try (InputStream is = zf.getInputStream(entry)) { // write file content
Files.copy(is, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); long fileSize = entry.getSize();
long bytesCopied = 0;
try (FileOutputStream fos = new FileOutputStream(newFile)) {
byte[] buffer = new byte[24576];
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
bytesCopied += len;
if (callback != null && fileSize > 0) {
callback.onFileProgress(bytesCopied, fileSize);
} }
setPermissionsFromMode(newFile.toPath(), entry.getUnixMode());
} }
} }
if (callback != null && fileSize > 0) {
callback.onFileProgress(fileSize, fileSize);
}
}
zis.closeEntry();
}
} }
} }