Skip to content

Commit

Permalink
Merge pull request #85 from zucisystems-dev/zip_file_agadia
Browse files Browse the repository at this point in the history
agadia zip file creation folder path fix
  • Loading branch information
Dineshkumar-Anandan-ZS0367 authored Mar 1, 2024
2 parents 6654ab0 + 66fe9ab commit 317af68
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/main/java/in/handyman/raven/lib/CreateZipAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -49,18 +47,58 @@ public void execute() throws Exception {
final String destFileDir = createZip.getDestination();
if (!Files.exists(Paths.get(source)))
log.info(aMarker, "{} source Folder not found", source);
FileOutputStream fos = new FileOutputStream(destFileDir + File.separator + zipFileName + ".zip");
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(source);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
// FileOutputStream fos = new FileOutputStream(destFileDir + File.separator + zipFileName + ".zip");
// ZipOutputStream zipOut = new ZipOutputStream(fos);
// File fileToZip = new File(source);
// zipFile(fileToZip, fileToZip.getName(), zipOut);
// zipOut.close();
// fos.close();
try {
zipFolderLegacy(source, destFileDir + File.separator + zipFileName + ".zip");
System.out.println("Folder successfully zipped.");
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
log.error(aMarker, "Error in execute method in create zip action {}", ExceptionUtil.toString(e));
throw new HandymanException("Error in execute method in create zip action", e, action);
}
}

private void zipFolderLegacy(String sourceFolderPath, String zipFilePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos)) {

File sourceFolder = new File(sourceFolderPath);
zipFileNew(sourceFolder, sourceFolder.getName(), zos);
}
}

private void zipFileNew(File fileToZip, String fileName, ZipOutputStream zos) throws IOException {
if (fileToZip.isHidden()) {
return;
}

if (fileToZip.isDirectory()) {
for (File file : fileToZip.listFiles()) {
zipFileNew(file, fileName + File.separator + file.getName(), zos);
}
return;
}

try (FileInputStream fis = new FileInputStream(fileToZip)) {
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zos.putNextEntry(zipEntry);

byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zos.write(buffer, 0, length);
}
}
}


public void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) {
try {
if (fileToZip.isHidden()) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/in/handyman/raven/lib/CreateZipActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package in.handyman.raven.lib;

import in.handyman.raven.lambda.doa.audit.ActionExecutionAudit;
import in.handyman.raven.lib.model.CreateZip;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.Map;

@Slf4j
class CreateZipActionTest {

@Test
void execute() throws Exception {
CreateZip createZipAction = CreateZip.builder().name("test")
.fileName("SYNT_166730538_c3")
.destination("/home/[email protected]/Documents/evaluation_output/47747/SYNT_166730538_c3")
.source("/home/[email protected]/Documents/evaluation_output/47747/SYNT_166730538_c3/SYNT_166730538_c3_29022024")
.build();

ActionExecutionAudit actionExecutionAudit = new ActionExecutionAudit();

actionExecutionAudit.getContext().putAll(Map.ofEntries(
Map.entry("read.batch.size", "5"),
Map.entry("gen_group_id.group_id", "5"),
Map.entry("consumer.API.count", "1"),
Map.entry("paper.itemizer.multipart.upload.url", "http://localhost:8002/multipart-download"),
Map.entry("write.batch.size", "5")));

CreateZipAction createZipAction1 = new CreateZipAction(actionExecutionAudit, log, createZipAction);
createZipAction1.execute();

}
}

0 comments on commit 317af68

Please sign in to comment.