/** * <p> * Converts a long into a BigInteger. Negative numbers between -1 and * -2^31 are treated as unsigned 32 bit (e.g., positive) integers. * Negative numbers below -2^31 cause an IllegalArgumentException * to be thrown. * </p> * * @param l long to convert to BigInteger. * @return BigInteger representation of the provided long. */ static BigInteger longToBig(long l) { if (l < Integer.MIN_VALUE) { throw new IllegalArgumentException("Negative longs < -2^31 not permitted: [" + l + "]"); } else if (l < 0 && l >= Integer.MIN_VALUE) { // If someone passes in a -2, they probably mean 4294967294 // (For example, Unix UID/GID's are 32 bit unsigned.) l = ZipUtil.adjustToLong((int) l); } return BigInteger.valueOf(l); }
/** * Gets the GID as a long. GID is typically a 32 bit unsigned * value on most UNIX systems, so we return a long to avoid * integer overflow into the negatives in case values above * and including 2^31 are being used. * * @return the GID value. */ public long getGID() { return ZipUtil.bigToLong(gid); }
/** * Whether this class is able to read the given entry. * * <p>May return false if it is set up to use encryption or a * compression method that hasn't been implemented yet.</p> * @since 1.1 * @param ze the entry * @return whether this class is able to read the given entry. */ public boolean canReadEntryData(final ZipArchiveEntry ze) { return ZipUtil.canHandleEntryData(ze); }
/** * Populate data from this array as if it was in local file data. * * @param data an array of bytes * @param offset the start offset * @param length the number of bytes in the array from offset * @throws java.util.zip.ZipException on error */ @Override public void parseFromLocalFileData( final byte[] data, int offset, final int length ) throws ZipException { reset(); this.version = signedByteToUnsignedInt(data[offset++]); final int uidSize = signedByteToUnsignedInt(data[offset++]); final byte[] uidBytes = new byte[uidSize]; System.arraycopy(data, offset, uidBytes, 0, uidSize); offset += uidSize; this.uid = new BigInteger(1, reverse(uidBytes)); // sign-bit forced positive final int gidSize = signedByteToUnsignedInt(data[offset++]); final byte[] gidBytes = new byte[gidSize]; System.arraycopy(data, offset, gidBytes, 0, gidSize); this.gid = new BigInteger(1, reverse(gidBytes)); // sign-bit forced positive }
reverse(uidBytes); reverse(gidBytes); data[pos++] = unsignedIntToSignedByte(version); data[pos++] = unsignedIntToSignedByte(uidBytesLen); if (uidBytes != null) { System.arraycopy(uidBytes, 0, data, pos, uidBytesLen); data[pos++] = unsignedIntToSignedByte(gidBytesLen); if (gidBytes != null) { System.arraycopy(gidBytes, 0, data, pos, gidBytesLen);
/** * The actual data to put into local file data. * * @return The LocalFileDataData value */ @Override public byte[] getLocalFileDataData() { return ZipUtil.copy(localFileData); }
off += SHORT; final long time = ZipUtil.dosToJavaTime(ZipLong.getValue(lfhBuf, off)); current.entry.setTime(time); off += WORD; ZipUtil.setNameAndCommentFromExtraFields(current.entry, fileName, null); if (ZipUtil.canHandleEntryData(current.entry) && m != ZipMethod.STORED && m != ZipMethod.DEFLATED) { InputStream bis = new BoundedInputStream(in, current.entry.getCompressedSize()); switch (m) {
private void copyFromZipInputStream(final InputStream src) throws IOException { if (entry == null) { throw new IllegalStateException("No current entry"); } ZipUtil.checkRequestedFeatures(entry.entry); entry.hasWritten = true; int length; while ((length = src.read(copyBuffer)) >= 0 ) { streamCompressor.writeCounted(copyBuffer, 0, length); count( length ); } }
/** * Convert a DOS date/time field to a Date object. * * @param zipDosTime contains the stored DOS time. * @return a Date instance corresponding to the given time. */ public static Date fromDosTime(final ZipLong zipDosTime) { final long dosTime = zipDosTime.getValue(); return new Date(dosToJavaTime(dosTime)); }
/** * Sets the UID. * * @param l UID value to set on this extra field. */ public void setUID(final long l) { this.uid = ZipUtil.longToBig(l); }
/** * If the entry has Unicode*ExtraFields and the CRCs of the * names/comments match those of the extra fields, transfer the * known Unicode values from the extra field. */ static void setNameAndCommentFromExtraFields(final ZipArchiveEntry ze, final byte[] originalNameBytes, final byte[] commentBytes) { final UnicodePathExtraField name = (UnicodePathExtraField) ze.getExtraField(UnicodePathExtraField.UPATH_ID); final String newName = getUnicodeStringIfOriginalMatches(name, originalNameBytes); if (newName != null) { ze.setName(newName); ze.setNameSource(ZipArchiveEntry.NameSource.UNICODE_EXTRA_FIELD); } if (commentBytes != null && commentBytes.length > 0) { final UnicodeCommentExtraField cmt = (UnicodeCommentExtraField) ze.getExtraField(UnicodeCommentExtraField.UCOM_ID); final String newComment = getUnicodeStringIfOriginalMatches(cmt, commentBytes); if (newComment != null) { ze.setComment(newComment); ze.setCommentSource(ZipArchiveEntry.CommentSource.UNICODE_EXTRA_FIELD); } } }
ZipUtil.setNameAndCommentFromExtraFields(ze, nc.name, nc.comment);
/** * Set the extra field data in central directory. * @param data the data to use */ public void setCentralDirectoryData(final byte[] data) { centralData = ZipUtil.copy(data); }
off += SHORT; final long time = ZipUtil.dosToJavaTime(ZipLong.getValue(lfhBuf, off)); current.entry.setTime(time); off += WORD; ZipUtil.setNameAndCommentFromExtraFields(current.entry, fileName, null); if (ZipUtil.canHandleEntryData(current.entry) && m != ZipMethod.STORED && m != ZipMethod.DEFLATED) { InputStream bis = new BoundedInputStream(in, current.entry.getCompressedSize()); switch (m) {
/** * Writes bytes to ZIP entry. * @param b the byte array to write * @param offset the start position to write from * @param length the number of bytes to write * @throws IOException on error */ @Override public void write(final byte[] b, final int offset, final int length) throws IOException { if (entry == null) { throw new IllegalStateException("No current entry"); } ZipUtil.checkRequestedFeatures(entry.entry); final long writtenThisTime = streamCompressor.write(b, offset, length, entry.entry.getMethod()); count(writtenThisTime); }
/** * Populate data from this array as if it was in local file data. * * @param data an array of bytes * @param offset the start offset * @param length the number of bytes in the array from offset * @throws java.util.zip.ZipException on error */ @Override public void parseFromLocalFileData( final byte[] data, int offset, final int length ) throws ZipException { reset(); this.version = signedByteToUnsignedInt(data[offset++]); final int uidSize = signedByteToUnsignedInt(data[offset++]); final byte[] uidBytes = new byte[uidSize]; System.arraycopy(data, offset, uidBytes, 0, uidSize); offset += uidSize; this.uid = new BigInteger(1, reverse(uidBytes)); // sign-bit forced positive final int gidSize = signedByteToUnsignedInt(data[offset++]); final byte[] gidBytes = new byte[gidSize]; System.arraycopy(data, offset, gidBytes, 0, gidSize); this.gid = new BigInteger(1, reverse(gidBytes)); // sign-bit forced positive }
reverse(uidBytes); reverse(gidBytes); data[pos++] = unsignedIntToSignedByte(version); data[pos++] = unsignedIntToSignedByte(uidBytesLen); if (uidBytes != null) { System.arraycopy(uidBytes, 0, data, pos, uidBytesLen); data[pos++] = unsignedIntToSignedByte(gidBytesLen); if (gidBytes != null) { System.arraycopy(gidBytes, 0, data, pos, gidBytesLen);
/** * The last modified date of the entry. * * <p>Note the interpretation of time is different depending on * the HostOS that has created the archive. While an OS that is * {@link #isHostOsUnix considered to be Unix} stores time in a * timezone independent manner, other platforms only use the local * time. I.e. if an archive has been created at midnight UTC on a * machine in timezone UTC this method will return midnight * regardless of timezone if the archive has been created on a * non-Unix system and a time taking the current timezone into * account if the archive has beeen created on Unix.</p> * * @return the last modified date */ @Override public Date getLastModifiedDate() { final long ts = isHostOsUnix() ? localFileHeader.dateTimeModified * 1000L : ZipUtil.dosToJavaTime(0xFFFFFFFFL & localFileHeader.dateTimeModified); return new Date(ts); }
/** * Sets the GID. * * @param l GID value to set on this extra field. */ public void setGID(final long l) { this.gid = ZipUtil.longToBig(l); }
/** * If the entry has Unicode*ExtraFields and the CRCs of the * names/comments match those of the extra fields, transfer the * known Unicode values from the extra field. */ static void setNameAndCommentFromExtraFields(final ZipArchiveEntry ze, final byte[] originalNameBytes, final byte[] commentBytes) { final UnicodePathExtraField name = (UnicodePathExtraField) ze.getExtraField(UnicodePathExtraField.UPATH_ID); final String newName = getUnicodeStringIfOriginalMatches(name, originalNameBytes); if (newName != null) { ze.setName(newName); ze.setNameSource(ZipArchiveEntry.NameSource.UNICODE_EXTRA_FIELD); } if (commentBytes != null && commentBytes.length > 0) { final UnicodeCommentExtraField cmt = (UnicodeCommentExtraField) ze.getExtraField(UnicodeCommentExtraField.UCOM_ID); final String newComment = getUnicodeStringIfOriginalMatches(cmt, commentBytes); if (newComment != null) { ze.setComment(newComment); ze.setCommentSource(ZipArchiveEntry.CommentSource.UNICODE_EXTRA_FIELD); } } }