diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractZipFileVerifier.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractZipFileVerifier.java new file mode 100644 index 0000000000000000000000000000000000000000..b1993b8a04e05a54811b27ac35cff821fecd0df1 --- /dev/null +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractZipFileVerifier.java @@ -0,0 +1,72 @@ +/* + * Copyright 2013 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.CRC32; + +import de.schlichtherle.util.zip.ZipFile; + +/** + * @author anttil + */ +public abstract class AbstractZipFileVerifier implements IArchiveFileVerifier +{ + + public abstract List<String> verify(ZipFile file); + + @Override + public final List<String> verify(File file) + { + List<String> errors = new ArrayList<String>(); + ZipFile zip; + try + { + zip = new ZipFile(file); + } catch (IOException ex) + { + errors.add("Reading zip file failed: " + ex.getMessage()); + return errors; + } + + return verify(zip); + } + + protected long calculateCRC32(InputStream input) throws IOException + { + BufferedInputStream inStream = new BufferedInputStream(input); + int BLOCK_SIZE = 128 * 1024; + int len; + byte[] buffer = new byte[BLOCK_SIZE]; + + CRC32 crc32 = new CRC32(); + crc32.reset(); + + while ((len = inStream.read(buffer, 0, BLOCK_SIZE)) > 0) + { + crc32.update(buffer, 0, len); + buffer = new byte[BLOCK_SIZE]; + } + + return crc32.getValue(); + } +} diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/DistributedPackagingDataSetFileOperationsManager.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/DistributedPackagingDataSetFileOperationsManager.java index 8c43ab849923a86288ef5e4cb0eba4b59f4b5b93..1dc1c0871e014baaee39380679eced0d60997bdf 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/DistributedPackagingDataSetFileOperationsManager.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/DistributedPackagingDataSetFileOperationsManager.java @@ -169,7 +169,7 @@ public class DistributedPackagingDataSetFileOperationsManager implements IDataSe List<String> errors = verify(file, new ZipFileIntegrityVerifier(), - new PathInfoCrcVerifier(new CrcProvider(datasetDescription.getDataSetCode()))); + new ZipFilePathInfoChecksumVerifier(new CrcProvider(datasetDescription.getDataSetCode()))); if (errors.size() > 0) { diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFileIntegrityVerifier.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFileIntegrityVerifier.java index b3055dcd8c41ed20c6556a503c60c3fcd30626b0..cde08ea3b82aa565f7c5546c496c7de19797a17c 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFileIntegrityVerifier.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFileIntegrityVerifier.java @@ -16,8 +16,6 @@ package ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard; -import java.io.BufferedInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -25,7 +23,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Enumeration; import java.util.List; -import java.util.zip.CRC32; import java.util.zip.ZipException; import de.schlichtherle.util.zip.ZipEntry; @@ -34,26 +31,13 @@ import de.schlichtherle.util.zip.ZipFile; /** * @author anttil */ -public class ZipFileIntegrityVerifier implements IArchiveFileVerifier +public class ZipFileIntegrityVerifier extends AbstractZipFileVerifier { @Override - public List<String> verify(File file) + public List<String> verify(ZipFile zip) { List<String> errors = new ArrayList<String>(); - ZipFile zip; - try - { - zip = new ZipFile(file); - } catch (ZipException ex) - { - errors.add("Reading zip file failed: " + ex.getMessage()); - return errors; - } catch (IOException ex) - { - errors.add("Reading zip file failed: " + ex.getMessage()); - return errors; - } Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) @@ -70,9 +54,7 @@ public class ZipFileIntegrityVerifier implements IArchiveFileVerifier try { input = zip.getInputStream(entry); - - long crc = calcCRC32(input); - + long crc = calculateCRC32(input); if (crc != entry.getCrc()) { return Arrays.asList(entry.getName() + ": CRC failure (got " + Long.toHexString(crc) + ", should be " @@ -99,23 +81,4 @@ public class ZipFileIntegrityVerifier implements IArchiveFileVerifier } return new ArrayList<String>(); } - - private static long calcCRC32(InputStream input) throws IOException - { - BufferedInputStream inStream = new BufferedInputStream(input); - int BLOCK_SIZE = 128 * 1024; - int len; - byte[] buffer = new byte[BLOCK_SIZE]; - - CRC32 crc32 = new CRC32(); - crc32.reset(); - - while ((len = inStream.read(buffer, 0, BLOCK_SIZE)) > 0) - { - crc32.update(buffer, 0, len); - buffer = new byte[BLOCK_SIZE]; - } - - return crc32.getValue(); - } } diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/PathInfoCrcVerifier.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFilePathInfoChecksumVerifier.java similarity index 71% rename from datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/PathInfoCrcVerifier.java rename to datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFilePathInfoChecksumVerifier.java index 25d6f8ed7302a06271ff050e43df2498d61277c1..92626ba00e776f72b9913ede70a56bb5d83a4d9d 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/PathInfoCrcVerifier.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/ZipFilePathInfoChecksumVerifier.java @@ -16,12 +16,9 @@ package ch.systemsx.cisd.openbis.dss.generic.server.plugins.standard; -import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import java.util.zip.ZipException; import de.schlichtherle.util.zip.ZipEntry; import de.schlichtherle.util.zip.ZipFile; @@ -29,34 +26,19 @@ import de.schlichtherle.util.zip.ZipFile; /** * @author anttil */ -public class PathInfoCrcVerifier implements IArchiveFileVerifier +public class ZipFilePathInfoChecksumVerifier extends AbstractZipFileVerifier { - private final ICrcProvider crcProvider; - public PathInfoCrcVerifier(ICrcProvider crcProvider) + public ZipFilePathInfoChecksumVerifier(ICrcProvider crcProvider) { this.crcProvider = crcProvider; } @Override - public List<String> verify(File file) + public List<String> verify(ZipFile zip) { List<String> errors = new ArrayList<String>(); - ZipFile zip; - try - { - zip = new ZipFile(file); - } catch (ZipException ex) - { - errors.add("Reading zip file failed: " + ex.getMessage()); - return errors; - } catch (IOException ex) - { - errors.add("Reading zip file failed: " + ex.getMessage()); - return errors; - } - Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { @@ -70,5 +52,4 @@ public class PathInfoCrcVerifier implements IArchiveFileVerifier } return errors; } - }