下面列出了java.util.zip.ZipEntry#getCrc ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void validateZipFile(ZipFile iviewImage) throws IOException {
Enumeration<? extends ZipEntry> entries = iviewImage.entries();
CRC32 crc = new CRC32();
byte[] data = new byte[4096];
int read;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
try (InputStream is = iviewImage.getInputStream(entry)) {
while ((read = is.read(data, 0, data.length)) != -1) {
crc.update(data, 0, read);
}
}
if (entry.getCrc() != crc.getValue()) {
throw new IOException("CRC32 does not match for entry: " + entry.getName());
}
crc.reset();
}
}
private String zipEntryDebugString(ZipEntry entryOne) {
return "<ZE name="
+ entryOne.getName()
+ " crc="
+ entryOne.getCrc()
+ " comment="
+ entryOne.getComment()
+ " size="
+ entryOne.getSize()
+ " atime="
+ entryOne.getLastAccessTime()
+ " mtime="
+ entryOne.getLastModifiedTime()
+ " ctime="
+ entryOne.getCreationTime()
+ ">";
}
private String zipEntryDebugString(ZipEntry entryOne) {
return "<ZE name="
+ entryOne.getName()
+ " crc="
+ entryOne.getCrc()
+ " comment="
+ entryOne.getComment()
+ " size="
+ entryOne.getSize()
+ " atime="
+ entryOne.getLastAccessTime()
+ " mtime="
+ entryOne.getLastModifiedTime()
+ " ctime="
+ entryOne.getCreationTime();
}
public ZippedEntry(ZipEntry e, InputStream is) {
this.name = e.getName();
this.time = e.getTime();
// this.mtime = e.getLastModifiedTime();
// this.atime = e.getLastAccessTime();
// this.ctime = e.getCreationTime();
this.crc = e.getCrc();
this.size = e.getSize();
this.csize = e.getCompressedSize();
this.method = e.getMethod();
this.extra = e.getExtra();
this.comment = e.getComment();
this.is = is;
}
@Override
public Boolean invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();
try (ZipFile zip = new ZipFile(f)) {
logger.print("Checking ");
logger.print(zip.size());
logger.print(" zipped entries in ");
logger.println(f.getAbsolutePath());
Checksum checksum = new CRC32();
byte[] buffer = new byte[4096];
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
checksum.reset();
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
try (InputStream inputStream = zip.getInputStream(entry)) {
int length;
while ((length = IOUtils.read(inputStream, buffer)) > 0) {
checksum.update(buffer, 0, length);
}
if (checksum.getValue() != entry.getCrc()) {
listener.error("Checksum error in : " + f.getAbsolutePath() + ":" + entry.getName());
return false;
}
}
}
}
return true;
} catch (ZipException e) {
listener.error("Error validating zip file: " + e.getMessage());
return false;
} finally {
logger.flush();
}
}
public ZippedEntry(ZipEntry e, InputStream is) {
this.name = e.getName();
this.time = e.getTime();
// this.mtime = e.getLastModifiedTime();
// this.atime = e.getLastAccessTime();
// this.ctime = e.getCreationTime();
this.crc = e.getCrc();
this.size = e.getSize();
this.csize = e.getCompressedSize();
this.method = e.getMethod();
this.extra = e.getExtra();
this.comment = e.getComment();
this.is = is;
}
public ZipContentItem( final ZipRepository repository,
final ZipContentLocation parent,
final ZipEntry zipEntry,
final byte[] bytes ) {
if ( repository == null ) {
throw new NullPointerException();
}
if ( zipEntry == null ) {
throw new NullPointerException();
}
if ( bytes == null ) {
throw new NullPointerException();
}
if ( parent == null ) {
throw new NullPointerException();
}
this.parent = parent;
this.repository = repository;
this.comment = zipEntry.getComment();
this.name = RepositoryUtilities.buildName( this, "/" );
this.entryName = IOUtils.getInstance().getFileName( name );
this.size = zipEntry.getSize();
this.crc32 = zipEntry.getCrc();
this.time = zipEntry.getTime();
this.rawData = bytes;
final int method = zipEntry.getMethod();
if ( method == ZipEntry.STORED || method == ZipEntry.DEFLATED ) {
this.method = new Integer( method );
} else {
this.method = new Integer( ZipEntry.DEFLATED );
}
this.compression = Deflater.DEFAULT_COMPRESSION;
}
public long getCrc(String pathRelativeToRoot) throws IOException {
try (ZipFile zipFile = new ZipFile(this.zipFile.toFile())) {
ZipEntry entry = zipFile.getEntry(pathRelativeToRoot);
long crc = entry.getCrc();
Preconditions.checkState(crc != -1, "Error accessing crc for entry: %s", pathRelativeToRoot);
return crc;
}
}
private ZipKey(ZipEntry entry, Path resolvedPath) {
this.resolvedPath = resolvedPath;
this.crc = entry.getCrc();
this.size = entry.getSize();
}