This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository tutti. See http://git.codelutin.com/tutti.git commit 59e93c7bfb3c13d5aee235cf40c57d02ee5b6db7 Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Mar 27 16:22:49 2015 +0100 decompresser l'archive d'import des le debut (on ne travaille plus sur les entrées du zip --- .../genericformat/GenericFormatArchive.java | 148 +++++++-------------- 1 file changed, 48 insertions(+), 100 deletions(-) diff --git a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java index 356fd27..d02da4d 100644 --- a/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java +++ b/tutti-service/src/main/java/fr/ifremer/tutti/service/genericformat/GenericFormatArchive.java @@ -24,18 +24,16 @@ package fr.ifremer.tutti.service.genericformat; * #L% */ -import com.google.common.base.Preconditions; import fr.ifremer.tutti.persistence.ProgressionModel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.commons.vfs2.FileObject; import org.nuiton.jaxx.application.ApplicationIOUtil; import org.nuiton.jaxx.application.ApplicationTechnicalException; +import org.nuiton.util.ZipUtil; import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.LineNumberReader; import java.io.Serializable; import java.nio.charset.Charset; @@ -45,8 +43,6 @@ import java.util.EnumMap; import java.util.EnumSet; import java.util.LinkedHashSet; import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; import static org.nuiton.i18n.I18n.t; @@ -85,7 +81,7 @@ public class GenericFormatArchive implements Serializable { DATA_MARINE_LITTER(true, "marineLitter.csv"), DATA_ACCIDENTAL_CATCH(true, "accidentalCatch.csv"), DATA_INDIVIDUAL_OBSERVATION(true, "individualObservation.csv"), - DATA_ATTACHMENTS(true, "attachments.csv") ; + DATA_ATTACHMENTS(true, "attachments.csv"); private final boolean mandatory; @@ -112,8 +108,6 @@ public class GenericFormatArchive implements Serializable { private final ArchiveMode archiveMode; - private final String prefixPath; - private final EnumMap<ArchiveFilePath, Integer> countLines; private final EnumSet<ArchiveFilePath> missingPaths; @@ -277,6 +271,10 @@ public class GenericFormatArchive implements Serializable { return countImportLines(ArchiveFilePath.DATA_ACCIDENTAL_CATCH); } + public int getAttachemntsPathLineCount() { + return countImportLines(ArchiveFilePath.DATA_ATTACHMENTS); + } + public int getParameterPathLineCount() { return countImportLines(ArchiveFilePath.DATA_PARAMETER); } @@ -339,10 +337,16 @@ public class GenericFormatArchive implements Serializable { } + public File extractAttachment(String path) { + + Path resolve = getAttachmentDataPath().resolve(path); + return resolve.toFile(); + + } + protected GenericFormatArchive(ArchiveMode archiveMode, File archiveFile, File workingDirectory) { this.archiveFile = archiveFile; - this.workingDirectory = workingDirectory; this.archiveMode = archiveMode; this.countLines = new EnumMap<>(ArchiveFilePath.class); @@ -352,20 +356,34 @@ public class GenericFormatArchive implements Serializable { log.info("Archive mode: " + archiveMode); } if (isImport()) { - this.prefixPath = extractPrefixPath(); - if (log.isInfoEnabled()) { - log.info("Zip entries prefix path: " + prefixPath); - } - missingPaths = computeMissingPaths(); + this.workingDirectory = extractArchive(archiveFile, workingDirectory); + this.missingPaths = computeMissingPaths(); } else { - this.prefixPath = null; + + this.workingDirectory = workingDirectory; this.missingPaths = null; + } } + protected File extractArchive(File archiveFile, File workingDirectory) { + + try { + ZipUtil.uncompress(archiveFile, workingDirectory); + } catch (IOException e) { + throw new ApplicationTechnicalException("Could not explode zipfile " + archiveFile, e); + } + File[] files = workingDirectory.listFiles(); + if (files==null || files.length!=1) { + throw new ApplicationTechnicalException("Archive should contains onyl one directory"); + } + return files[0]; + + } + protected boolean isImport() { return ArchiveMode.IMPORT == archiveMode; } @@ -374,114 +392,44 @@ public class GenericFormatArchive implements Serializable { return ArchiveMode.EXPORT == archiveMode; } - protected String extractPrefixPath() { - - FileObject fileObject = ApplicationIOUtil.resolveFile( - "zip:" + archiveFile.getAbsolutePath(), - t("tutti.service.persistence.getArchive.error", archiveFile)); - - FileObject[] children = ApplicationIOUtil.getChildren( - fileObject, - t("tutti.service.persistence.openArchive.error", fileObject)); - - fileObject = children[0]; - - String prefix = fileObject.getName().getBaseName(); - return prefix; - - } - protected Path getPath(ArchiveFilePath archiveFilePath) { String filename = archiveFilePath.getFilename(); Path file = workingDirectory.toPath().resolve(filename); - if (isImport() && Files.notExists(file) && !missingPaths.contains(archiveFilePath)) { - - // Explode from archive - - try (ZipFile zipFile = new ZipFile(archiveFile)) { - - ZipEntry entry = getZipEntryPath(archiveFilePath, zipFile); - - Preconditions.checkState(!(archiveFilePath.isMandatory() && entry == null), "Must have entry " + filename); - - if (entry != null) { - - if (log.isInfoEnabled()) { - log.info("Explode zip entry to " + file.toFile().getName()); - } - - try (InputStream inputStream = zipFile.getInputStream(entry)) { - Files.copy(inputStream, file); - } - - } - - } catch (IOException e) { - throw new ApplicationTechnicalException("Could not open zip file: " + archiveFile, e); - } - - } - return file; } - protected ZipEntry getZipEntryPath(ArchiveFilePath archiveFilePath, ZipFile zipFile ) { - String path = prefixPath + "/" + archiveFilePath.getFilename(); - ZipEntry entry = zipFile.getEntry(path); - if (entry==null) { - path = prefixPath + "\\" + archiveFilePath.getFilename(); - entry = zipFile.getEntry(path); - } - return entry; - } - - protected String getZipEntryPath(ArchiveFilePath archiveFilePath) { - - String path = prefixPath + archiveFilePath.getFilename(); - return path; - - } - protected EnumSet<ArchiveFilePath> computeMissingPaths() { EnumSet<ArchiveFilePath> result = EnumSet.noneOf(ArchiveFilePath.class); - try (ZipFile zipFile = new ZipFile(archiveFile)) { - - for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { + for (ArchiveFilePath archiveFilePath : ArchiveFilePath.values()) { - ZipEntry zipEntry = getZipEntryPath(archiveFilePath, zipFile); + Path path = getPath(archiveFilePath); - if (log.isDebugEnabled()) { - log.debug("Check if entry " + archiveFilePath + " exists."); - } + if (log.isDebugEnabled()) { + log.debug("Check if entry " + archiveFilePath + " exists."); + } - if (zipEntry == null) { + if (!Files.exists(path)) { - if (log.isInfoEnabled()) { - log.info("Entry " + archiveFilePath + " not found."); - } - - result.add(archiveFilePath); + if (log.isInfoEnabled()) { + log.info("Entry " + archiveFilePath + " not found."); + } - } else { + result.add(archiveFilePath); - if (log.isInfoEnabled()) { - log.info("Entry " + archiveFilePath + " found."); - } + } else { + if (log.isInfoEnabled()) { + log.info("Entry " + archiveFilePath + " found."); } } - } catch (IOException e) { - - throw new ApplicationTechnicalException("Could not open or close zip file: " + archiveFile, e); - } return result; @@ -495,9 +443,9 @@ public class GenericFormatArchive implements Serializable { Path path = getPath(archiveFilePath); - if (path==null) { + if (path == null) { - result=0; + result = 0; countLines.put(archiveFilePath, 0); } else { -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.