You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/09 16:31:04 UTC

[GitHub] [spark] zhongjingxiong commented on a change in pull request #35278: [SPARK-37677][CORE] Decompress the ZIP file and retain the original file permissions

zhongjingxiong commented on a change in pull request #35278:
URL: https://github.com/apache/spark/pull/35278#discussion_r802859443



##########
File path: core/src/main/scala/org/apache/spark/util/Utils.scala
##########
@@ -607,6 +609,66 @@ private[spark] object Utils extends Logging {
     }
   }
 
+  def unZip(inFile: File, unzipDir: File): Unit = {
+    val zipFile = new ZipFile(inFile)
+    try {
+      val entries = zipFile.getEntries
+      val targetDirPath = unzipDir.getCanonicalPath + File.separator
+      while ({entries.hasMoreElements}) {
+        val entry: ZipArchiveEntry = entries.nextElement
+        if (!entry.isDirectory) {
+          val in = zipFile.getInputStream(entry)
+          try {
+            val file = new File(unzipDir, entry.getName)
+            if (!file.getCanonicalPath.startsWith(targetDirPath)) {
+              throw new IOException(
+                "expanding " + entry.getName + " would create file outside of " + unzipDir
+              )
+            }
+            if (!file.getParentFile.mkdirs && !file.getParentFile.isDirectory) {
+              throw new IOException("Mkdirs failed to create " + file.getParentFile.toString)
+            }
+
+            val out = Files.newOutputStream(file.toPath)
+            try {
+              val buffer = new Array[Byte](8192)
+              var len = 0
+              while ({len = in.read(buffer); len} != -1) {
+                out.write(buffer, 0, len)
+              }
+            }
+            finally {
+              out.close()
+              if (entry.getPlatform == ZipArchiveEntry.PLATFORM_UNIX) {

Review comment:
       Here, if the file has permission settings before decompression, we will set the permission back after decompressing the file.




-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org