aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/utils')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java55
-rw-r--r--src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java30
2 files changed, 60 insertions, 25 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
index 88e597b..1862f20 100644
--- a/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
+++ b/src/main/java/at/gv/egiz/pdfas/utils/ConfigUtils.java
@@ -1,8 +1,6 @@
package at.gv.egiz.pdfas.utils;
-import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -81,11 +79,37 @@ public final class ConfigUtils {
if (in == null) {
throw new ConfigUtilsException("Unable to find default configuration resource \"" + Constants.DEFAULT_CONFIGURATION_ZIP_RESOURCE + "\".");
}
- ZipInputStream zis = new ZipInputStream(in);
+ return deployFromZIP(in, destination, overwriteExisting);
+ } catch (IOException e) {
+ throw new ConfigUtilsException(e);
+ }
+ }
+
+ /**
+ * Deploys the contents of a ZIP file to a certain location.
+ *
+ * @param inputStream The inputStream of a ZIP container.
+ * @param destination The destination folder.
+ * @param overwriteExisting If set <code>true</code> an already existing configuration is overwritten. If <code>false</code> nothing is being copied if the destination folder already exists.
+ * @return <code>true</code> if the configuration has been deployed, <code>false</code> if not.
+ * @throws ConfigUtilsException Thrown if there was an error during the deployment of the configuration.
+ */
+ public static boolean deployFromZIP(InputStream inputStream, String destination, boolean overwriteExisting) throws ConfigUtilsException {
+ try {
+ if (!overwriteExisting) {
+ if (configurationAlreadyExists(destination)) {
+ logger_.debug("There is at least one file or folder that would be overwritten at destination path \"" + destination + "\". Skipping extraction.");
+ return false;
+ }
+ }
+ if (inputStream == null) {
+ throw new ConfigUtilsException("Unable to deploy ZIP file. InputStream is null.");
+ }
+ ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry ze;
File destinationFolder = new File(destination);
destinationFolder.mkdirs();
- logger_.debug("Extracting default configuration to folder \"" + destinationFolder.getCanonicalPath() + "\".");
+ logger_.debug("Extracting ZIP contents to folder \"" + destinationFolder.getCanonicalPath() + "\".");
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
File newFolder = new File(destinationFolder, ze.getName());
@@ -94,7 +118,7 @@ public final class ConfigUtils {
} else {
File destFile = new File(destinationFolder, ze.getName());
logger_.trace("Extracting file \"" + destFile.getName() + "\".");
- toFile(zis, destFile);
+ PDFASUtils.toFile(zis, destFile);
}
zis.closeEntry();
}
@@ -104,7 +128,7 @@ public final class ConfigUtils {
throw new ConfigUtilsException(e);
}
}
-
+
private static boolean configurationAlreadyExists(String destination) throws ConfigUtilsException, IOException {
logger_.debug("Checking configuration \"" + destination + "\" already exists (resp. if there are any directories or files that would be overwritten).");
File destinationFolder = new File(destination);
@@ -196,25 +220,6 @@ public final class ConfigUtils {
return deployDefaultConfiguration(false);
}
- public static boolean toFile(InputStream inputStream, File file) throws IOException {
- boolean result = false;
- BufferedOutputStream bufferedOutputStream = null;
- try {
- bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
- writeInputStreamToOutputStream(inputStream, bufferedOutputStream);
- } finally {
- if (bufferedOutputStream != null) {
- try {
- bufferedOutputStream.close();
- result = true;
- } catch (IOException e) {
- result = false;
- }
- }
- }
- return result;
- }
-
public static void writeInputStreamToOutputStream(InputStream inputStream, OutputStream outputStream) throws IOException {
final int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
diff --git a/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java
index b26cc9b..9841779 100644
--- a/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java
+++ b/src/main/java/at/gv/egiz/pdfas/utils/PDFASUtils.java
@@ -1,5 +1,12 @@
package at.gv.egiz.pdfas.utils;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
import at.gv.egiz.pdfas.exceptions.ErrorCode;
import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
@@ -26,5 +33,28 @@ public class PDFASUtils {
throw new PDFDocumentException(ErrorCode.DOCUMENT_IS_PROTECTED, "Document is protected.");
}
}
+
+ public static boolean toFile(byte[] data, File file) throws IOException {
+ return PDFASUtils.toFile(new ByteArrayInputStream(data), file);
+ }
+
+ public static boolean toFile(InputStream inputStream, File file) throws IOException {
+ boolean result = false;
+ BufferedOutputStream bufferedOutputStream = null;
+ try {
+ bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
+ ConfigUtils.writeInputStreamToOutputStream(inputStream, bufferedOutputStream);
+ } finally {
+ if (bufferedOutputStream != null) {
+ try {
+ bufferedOutputStream.close();
+ result = true;
+ } catch (IOException e) {
+ result = false;
+ }
+ }
+ }
+ return result;
+ }
}