You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by GitBox <gi...@apache.org> on 2021/01/25 18:20:47 UTC

[GitHub] [incubator-daffodil] tuxji commented on a change in pull request #480: WIP: First cut at a Zip layer transform

tuxji commented on a change in pull request #480:
URL: https://github.com/apache/incubator-daffodil/pull/480#discussion_r563807688



##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/layers/ZipTransformer.scala
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.daffodil.layers
+
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
+import org.apache.commons.compress.archivers.zip.ZipFile
+import org.apache.commons.compress.utils.SeekableInMemoryByteChannel
+import org.apache.commons.io.IOUtils

Review comment:
       For curiosity, I googled "best java zip library" and found some [alternatives](https://stackoverflow.com/questions/5362364/java-library-to-work-with-zip-files).  However, it looks like the Apache Compress library has multiple maintainers (not just one maintainer), supports nearly every feature of other zip libraries except encrypted zip files, documents its [interoperation](https://commons.apache.org/proper/commons-compress/zip.html) with other zip programs, and lists its known [limitations](https://commons.apache.org/proper/commons-compress/limitations.html) so it looks like the best choice (it also supports other compression formats too).

##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/layers/ZipTransformer.scala
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.daffodil.layers
+
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
+import org.apache.commons.compress.archivers.zip.ZipFile
+import org.apache.commons.compress.utils.SeekableInMemoryByteChannel
+import org.apache.commons.io.IOUtils
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthKind
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthUnits
+import org.apache.daffodil.util.Maybe
+import org.apache.daffodil.processors.LayerLengthInBytesEv
+import org.apache.daffodil.processors.LayerBoundaryMarkEv
+import org.apache.daffodil.processors.LayerCharsetEv
+import org.apache.daffodil.processors.parsers.PState
+import org.apache.daffodil.processors.unparsers.UState
+import org.apache.daffodil.dsom.DPathCompileInfo
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.io.ExplicitLengthLimitingStream
+
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import java.io.InputStream
+import java.io.OutputStream
+import java.io.SequenceInputStream
+import java.nio.charset.StandardCharsets
+import scala.Array.emptyByteArray
+import scala.collection.JavaConverters._
+
+/*
+ * Zip files require seeking to the end of the file. Hence, we need to have an explicit
+ * length for a zip layer to work from.
+ *
+ * When writing out zip file contents, to get the length, one must take the
+ * dfdl:contentLength of an enclosing element that surrounds the entire layer.
+ *
+ * All of this forces buffering. So there's no point in worrying about
+ * somehow avoiding buffering inside the implementation.
+ * That can simplify many things. There's no point, for example, in trying
+ * to not read and buffer all the entries.
+ *
+ */

Review comment:
       Please consider merging/rearranging the preceding and following comments.  I believe scaladoc will ignore the preceding comment right now.

##########
File path: daffodil-runtime1/src/main/scala/org/apache/daffodil/layers/ZipTransformer.scala
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.daffodil.layers
+
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
+import org.apache.commons.compress.archivers.zip.ZipFile
+import org.apache.commons.compress.utils.SeekableInMemoryByteChannel
+import org.apache.commons.io.IOUtils
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthKind
+import org.apache.daffodil.schema.annotation.props.gen.LayerLengthUnits
+import org.apache.daffodil.util.Maybe
+import org.apache.daffodil.processors.LayerLengthInBytesEv
+import org.apache.daffodil.processors.LayerBoundaryMarkEv
+import org.apache.daffodil.processors.LayerCharsetEv
+import org.apache.daffodil.processors.parsers.PState
+import org.apache.daffodil.processors.unparsers.UState
+import org.apache.daffodil.dsom.DPathCompileInfo
+import org.apache.daffodil.exceptions.Assert
+import org.apache.daffodil.io.ExplicitLengthLimitingStream
+
+import java.io.ByteArrayInputStream
+import java.io.ByteArrayOutputStream
+import java.io.InputStream
+import java.io.OutputStream
+import java.io.SequenceInputStream
+import java.nio.charset.StandardCharsets
+import scala.Array.emptyByteArray
+import scala.collection.JavaConverters._
+
+/*
+ * Zip files require seeking to the end of the file. Hence, we need to have an explicit
+ * length for a zip layer to work from.
+ *
+ * When writing out zip file contents, to get the length, one must take the
+ * dfdl:contentLength of an enclosing element that surrounds the entire layer.
+ *
+ * All of this forces buffering. So there's no point in worrying about
+ * somehow avoiding buffering inside the implementation.
+ * That can simplify many things. There's no point, for example, in trying
+ * to not read and buffer all the entries.

Review comment:
       I agree.  The Apache Compress library works best with seekable byte channels passed to ZipFile (input) or ZipArchiveOutputStream (output).  I see you're using these seekable byte channels, which is good.  If we need to handle very large files, we could use disk files instead of in-memory buffers.




----------------------------------------------------------------
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:
users@infra.apache.org