-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hack to handle special characters so we can release
- Loading branch information
Showing
5 changed files
with
124 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ out | |
.idea | ||
.settings | ||
.classpath | ||
tokens | ||
*credentials.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.kpmp.zip; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; | ||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ZipService { | ||
|
||
private static final String TMP_FILE_EXTENSION = ".tmp"; | ||
|
||
public void createZipFile(String zipFilePath, List<String> filePaths, Map<String, String> additionalFileInformation) | ||
throws IOException { | ||
File tempZipFileHandle = new File(zipFilePath + TMP_FILE_EXTENSION); | ||
try (ZipArchiveOutputStream zipFile = new ZipArchiveOutputStream(new File(zipFilePath))) { | ||
zipFile.setMethod(ZipArchiveOutputStream.DEFLATED); | ||
zipFile.setEncoding(StandardCharsets.UTF_8.name()); | ||
for (String filePath : filePaths) { | ||
File file = new File(filePath); | ||
ZipArchiveEntry entry = new ZipArchiveEntry(file.getName()); | ||
entry.setSize(file.length()); | ||
zipFile.putArchiveEntry(entry); | ||
try (FileInputStream fileInputStream = new FileInputStream(file)) { | ||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) { | ||
byte[] buffer = new byte[32768]; | ||
int data = 0; | ||
while ((data = bufferedInputStream.read(buffer, 0, buffer.length)) != -1) { | ||
zipFile.write(buffer, 0, data); | ||
} | ||
zipFile.flush(); | ||
bufferedInputStream.close(); | ||
} | ||
fileInputStream.close(); | ||
} | ||
zipFile.closeArchiveEntry(); | ||
} | ||
|
||
Set<String> keys = additionalFileInformation.keySet(); | ||
for (String additionalFileName : keys) { | ||
ZipArchiveEntry additionalEntry = new ZipArchiveEntry(additionalFileName); | ||
String additionaFileContents = additionalFileInformation.get(additionalFileName); | ||
additionalEntry.setSize(additionaFileContents.getBytes().length); | ||
zipFile.putArchiveEntry(additionalEntry); | ||
zipFile.write(additionaFileContents.getBytes(StandardCharsets.UTF_8)); | ||
zipFile.closeArchiveEntry(); | ||
} | ||
|
||
File zipFileHandle = new File(zipFilePath); | ||
tempZipFileHandle.renameTo(zipFileHandle); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters