Skip to content

Commit

Permalink
Merge pull request #420 from checkmarx-ltd/develop
Browse files Browse the repository at this point in the history
Added symbolic link zip feature
  • Loading branch information
satyamchaurasiapersistent authored Sep 11, 2024
2 parents 001c941 + c84c29f commit c6bee63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.github.checkmarx-ltd</groupId>
<artifactId>cx-spring-boot-sdk</artifactId>
<version>0.6.14</version>
<version>0.6.15</version>


<name>cx-spring-boot-sdk</name>
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/checkmarx/sdk/utils/zip/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -40,23 +42,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);
addToZip("", String.format("%s/%s", fileToZip, fileName), zipFile, zipOut, excludeList,fileToZip);
}
} else {
addToZip("", fileToZip, zipFile, zipOut, excludeList);
addToZip("", fileToZip, zipFile, zipOut, excludeList,fileToZip);
}
zipOut.flush();
}
log.info("Successfully created {} ", zipFile);
}

private static void addToZip(String path, String srcFile, String zipFile, ZipOutputStream zipOut, List<String> excludePatterns)
private static void addToZip(String path, String srcFile, String zipFile, ZipOutputStream zipOut, List<String> excludePatterns,String rootDir)
throws IOException {
File file = new File(srcFile);
String filePath = "".equals(path) ? file.getName() : String.format("%s/%s", path, file.getName());
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();
}
if (file.isDirectory()) {
for (String fileName : Objects.requireNonNull(file.list())) {
addToZip(filePath, srcFile + "/" + fileName, zipFile, zipOut, excludePatterns);
addToZip(filePath, srcFile + "/" + fileName, zipFile, zipOut, excludePatterns,rootDir);
}
} else {
String tmpPath = FileSystems.getDefault().getPath(srcFile).toAbsolutePath().toString();
Expand Down

0 comments on commit c6bee63

Please sign in to comment.