@Override public ZipShort getCentralDirectoryLength() { return new ZipShort((size != null ? DWORD : 0) + (compressedSize != null ? DWORD : 0) + (relativeHeaderOffset != null ? DWORD : 0) + (diskStart != null ? WORD : 0)); }
/** * 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 { final int len = offset + length; // skip reserved offset += 4; while (offset + 4 <= len) { final ZipShort tag = new ZipShort(data, offset); offset += 2; if (tag.equals(TIME_ATTR_TAG)) { readTimeAttr(data, offset, len - offset); break; } final ZipShort size = new ZipShort(data, offset); offset += 2 + size.getValue(); } }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * * @return get the data */ @Override public byte[] getLocalFileDataData() { final byte[] data = new byte[getLocalFileDataLength().getValue()]; int pos = 4; System.arraycopy(TIME_ATTR_TAG.getBytes(), 0, data, pos, 2); pos += 2; System.arraycopy(TIME_ATTR_SIZE.getBytes(), 0, data, pos, 2); pos += 2; System.arraycopy(modifyTime.getBytes(), 0, data, pos, 8); pos += 8; System.arraycopy(accessTime.getBytes(), 0, data, pos, 8); pos += 8; System.arraycopy(createTime.getBytes(), 0, data, pos, 8); return data; }
private void readTimeAttr(final byte[] data, int offset, final int length) { if (length >= 2 + 3 * 8) { final ZipShort tagValueLength = new ZipShort(data, offset); if (TIME_ATTR_SIZE.equals(tagValueLength)) { offset += 2; modifyTime = new ZipEightByteInteger(data, offset); offset += 8; accessTime = new ZipEightByteInteger(data, offset); offset += 8; createTime = new ZipEightByteInteger(data, offset); } } }
/** * Create instance from the two bytes starting at offset. * @param bytes the bytes to store as a ZipShort * @param offset the offset to start */ public ZipShort (final byte[] bytes, final int offset) { value = ZipShort.getValue(bytes, offset); }
/** * Get value as two bytes in big endian byte order. * @param value the Java int to convert to bytes * @return the converted int as a byte array in big endian byte order */ public static byte[] getBytes(final int value) { final byte[] result = new byte[2]; putShort(value, result, 0); return result; }
@Override public byte[] getCentralDirectoryData() { return ZipShort.getBytes(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0)); }
/** * Looks up an extra field by its header id. * * @param type the header id * @return null if no such field exists. */ public ZipExtraField getExtraField(final ZipShort type) { if (extraFields != null) { for (final ZipExtraField extraField : extraFields) { if (type.equals(extraField.getHeaderId())) { return extraField; } } } return null; }
/** * Override to make two instances with same value equal. * @param o an object to compare * @return true if the objects are equal */ @Override public boolean equals(final Object o) { if (o == null || !(o instanceof ZipShort)) { return false; } return value == ((ZipShort) o).getValue(); }
private void readTimeAttr(final byte[] data, int offset, final int length) { if (length >= 2 + 3 * 8) { final ZipShort tagValueLength = new ZipShort(data, offset); if (TIME_ATTR_SIZE.equals(tagValueLength)) { offset += 2; modifyTime = new ZipEightByteInteger(data, offset); offset += 8; accessTime = new ZipEightByteInteger(data, offset); offset += 8; createTime = new ZipEightByteInteger(data, offset); } } }
@Override public byte[] getLocalFileDataData() { byte[] content = new byte[BASE_SIZE + padding]; ZipShort.putShort(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0), content, 0); return content; }
/** * Remove an extra field. * @param type the type of extra field to remove */ public void removeExtraField(final ZipShort type) { if (extraFields == null) { throw new java.util.NoSuchElementException(); } final List<ZipExtraField> newResult = new ArrayList<>(); for (final ZipExtraField extraField : extraFields) { if (!type.equals(extraField.getHeaderId())){ newResult.add( extraField); } } if (extraFields.length == newResult.size()) { throw new java.util.NoSuchElementException(); } extraFields = newResult.toArray(new ZipExtraField[newResult.size()]); setExtra(); }
/** * Length of the complete extra field in the local file data. * * @return The LocalFileDataLength value */ @Override public ZipShort getLocalFileDataLength() { return new ZipShort(localFileData == null ? 0 : localFileData.length); }
/** * The actual data to put into local file data - without Header-ID * or length specifier. * @return get the data */ @Override public byte[] getLocalFileDataData() { // CRC will be added later final byte[] data = new byte[getLocalFileDataLength().getValue() - WORD]; System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2); final byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc // CheckStyle:MagicNumber OFF System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data, 2, WORD); System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2); System.arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8, 2); System.arraycopy(linkArray, 0, data, 10, linkArray.length); // CheckStyle:MagicNumber ON crc.reset(); crc.update(data); final long checksum = crc.getValue(); final byte[] result = new byte[data.length + WORD]; System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD); System.arraycopy(data, 0, result, WORD, data.length); return result; }
/** * Helper method to get the value as a java int from a two-byte array * @param bytes the array of bytes * @return the corresponding java int value */ public static int getValue(final byte[] bytes) { return getValue(bytes, 0); }
/** * 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 { final int len = offset + length; // skip reserved offset += 4; while (offset + 4 <= len) { final ZipShort tag = new ZipShort(data, offset); offset += 2; if (tag.equals(TIME_ATTR_TAG)) { readTimeAttr(data, offset, len - offset); break; } final ZipShort size = new ZipShort(data, offset); offset += 2 + size.getValue(); } }
/** * Encodes the set bits in a form suitable for ZIP archives. * * @param buf the output buffer * @param offset * The offset within the output buffer of the first byte to be written. * must be non-negative and no larger than <tt>buf.length-2</tt> */ public void encode(final byte[] buf, final int offset) { ZipShort.putShort((dataDescriptorFlag ? DATA_DESCRIPTOR_FLAG : 0) | (languageEncodingFlag ? UFT8_NAMES_FLAG : 0) | (encryptionFlag ? ENCRYPTION_FLAG : 0) | (strongEncryptionFlag ? STRONG_ENCRYPTION_FLAG : 0) , buf, offset); }