Skip to content

Commit

Permalink
Added Symbolic link zip feature (#1386)
Browse files Browse the repository at this point in the history
* Added Symbolic link zip feature

* Update build.gradle

* Update ZipUtils.java
  • Loading branch information
itsKedar authored Oct 1, 2024
1 parent d145a83 commit 5e86a9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import org.gradle.api.tasks.testing.Test

buildscript {
ext {
CxSBSDK = "0.6.14"
CxSBSDK = "0.6.15"
ConfigProviderVersion = '1.0.14'
//cxVersion = "8.90.5"
springBootVersion = '3.2.5'
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/com/checkmarx/flow/utils/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -58,23 +60,39 @@ public static void zipFile(String fileToZip, String zipFile, String excludePatte
File srcFile = new File(fileToZip);
if (srcFile.isDirectory()) {
for (String fileName : Objects.requireNonNull(srcFile.list())) {
addToZip("", String.format("%s/%s", fileToZip, fileName), zipFile, zipOut, excludeList,includeList);
addToZip("", String.format("%s/%s", fileToZip, fileName), zipFile, zipOut, excludeList,includeList,fileToZip);
}
} else {
addToZip("", fileToZip, zipFile, zipOut, excludeList,includeList);
addToZip("", fileToZip, zipFile, zipOut, excludeList,includeList,fileToZip);
}
zipOut.flush();
}
log.info("Successfully created {} ", zipFile);
}

private static void addToZip(String path, String srcFile, String zipFile, ZipOutputStream zipOut, List<String> excludePatterns,List<String> includePatterns)

private static void addToZip(String path, String srcFile, String zipFile, ZipOutputStream zipOut, List<String> excludePatterns,List<String> includePatterns,String rootDir)
throws IOException {
File file = new File(srcFile);
String filePath = "".equals(path) ? file.getName() : String.format("%s/%s", path, file.getName());
if (file.isDirectory()) {
Path filePathObj = file.toPath();
if (Files.isSymbolicLink(filePathObj)) {
Path targetPath = Files.readSymbolicLink(filePathObj);
String targetPathStr = targetPath.toAbsolutePath().toString();

// Check if the symbolic link points within the directory being zipped
if (targetPathStr.startsWith(new File(rootDir).getAbsolutePath())) {
log.debug("#########Skipping symbolic link {} pointing to {}#########", filePath, targetPathStr);
return;
}

// Add the symbolic link entry to the zip
zipOut.putNextEntry(new ZipEntry(filePath));
zipOut.write(targetPathStr.getBytes());
zipOut.closeEntry();
}else if (file.isDirectory()) {
for (String fileName : Objects.requireNonNull(file.list())) {
addToZip(filePath, srcFile + "/" + fileName, zipFile, zipOut, excludePatterns,includePatterns);
addToZip(filePath, srcFile + "/" + fileName, zipFile, zipOut, excludePatterns,includePatterns,rootDir);
}
} else {
String tmpPath = FileSystems.getDefault().getPath(srcFile).toAbsolutePath().toString();
Expand Down

0 comments on commit 5e86a9d

Please sign in to comment.