bodewig commented on a change in pull request #84: COMPRESS-477 Add support for extracting splitted zip files URL: https://github.com/apache/commons-compress/pull/84#discussion_r341816134
########## File path: src/main/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannel.java ########## @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.commons.compress.utils; + +import org.apache.commons.compress.archivers.ArchiveStreamFactory; +import org.apache.commons.compress.archivers.zip.ZipLong; +import org.apache.commons.compress.compressors.FileNameUtil; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; + +public class ZipSplitReadOnlySeekableByteChannel extends MultiReadOnlySeekableByteChannel { + protected final int ZIP_SPLIT_SIGNATURE_LENGTH = 4; + protected ByteBuffer zipSplitSignatureByteBuffer = ByteBuffer.allocate(ZIP_SPLIT_SIGNATURE_LENGTH); + + /** + * Concatenates the given channels. + * the channels should be add in ascending order, e.g. z01, z02, ... z99, zip + * please note that the .zip file is the last segment and should be added as the last one in the channels + * + * The first 4 bytes of split zip signature will be taken into consideration by Inflator, + * so we don't need to skip them + * + * @param channels the channels to concatenate + * @throws NullPointerException if channels is null + */ + public ZipSplitReadOnlySeekableByteChannel(List<SeekableByteChannel> channels) throws IOException { + super(channels); + + // each split zip segment should begin with zip split signature + validSplitSignature(channels); + } + + /** + * the first 4 bytes of zip split segments should be the zip split signature(0x08074B50) + * + * @param channels channels to be valided + * @throws IOException + */ + private void validSplitSignature(final List<SeekableByteChannel> channels) throws IOException { + for(int i = 0;i < channels.size();i++) { + SeekableByteChannel channel = channels.get(i); + // the zip split file signature is always at the beginning of the segment + channel.position(0L); + + channel.read(zipSplitSignatureByteBuffer); + final ZipLong signature = new ZipLong(zipSplitSignatureByteBuffer.array()); + if(!signature.equals(ZipLong.DD_SIG)) { + channel.position(0L); + throw new IOException("No." + (i + 1) + " split zip file is not begin with split zip file signature"); + } + + channel.position(0L); + } + } + + /** + * set the position based on the given disk number and relative offset + * + * @param diskNumber the disk number of split zip files + * @param relativeOffset the offset in the split zip segment with given disk number + * @return global position of all split zip files as if they are a single channel + * @throws IOException + */ + public synchronized SeekableByteChannel position(long diskNumber, long relativeOffset) throws IOException { Review comment: this method could live inside of the superclass (with diskNumber renamed to channelNumber or something like that). I think that would avoid making the fields protected. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
