You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Unzip.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.util;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.util.zip.ZipEntry;
  25. import java.util.zip.ZipException;
  26. import java.util.zip.ZipInputStream;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. /**
  30. * This class extracts the content of the plugin zip into a directory.
  31. * It's a class for only the internal use.
  32. *
  33. * @author Decebal Suiu
  34. */
  35. public class Unzip {
  36. private static final Logger log = LoggerFactory.getLogger(Unzip.class);
  37. /**
  38. * Holds the destination directory.
  39. * File will be unzipped into the destination directory.
  40. */
  41. private File destination;
  42. /**
  43. * Holds path to zip file.
  44. */
  45. private File source;
  46. public Unzip() {
  47. }
  48. public Unzip(File source, File destination) {
  49. this.source = source;
  50. this.destination = destination;
  51. }
  52. public void setSource(File source) {
  53. this.source = source;
  54. }
  55. public void setDestination(File destination) {
  56. this.destination = destination;
  57. }
  58. /**
  59. * Extract the content of zip file ({@code source}) to destination directory.
  60. * If destination directory already exists it will be deleted before.
  61. */
  62. public void extract() throws IOException {
  63. log.debug("Extract content of '{}' to '{}'", source, destination);
  64. // delete destination directory if exists
  65. if (destination.exists() && destination.isDirectory()) {
  66. FileUtils.delete(destination.toPath());
  67. }
  68. try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
  69. ZipEntry zipEntry;
  70. while ((zipEntry = zipInputStream.getNextEntry()) != null) {
  71. File file = new File(destination, zipEntry.getName());
  72. // add check
  73. if (zipEntry.getName().indexOf("..") != -1 && !file.getCanonicalPath().startsWith(destination.getCanonicalPath())) {
  74. throw new ZipException("The file "+zipEntry.getName()+" is trying to leave the target output directory of "+destination+". Ignoring this file.");
  75. }
  76. // create intermediary directories - sometimes zip don't add them
  77. File dir = new File(file.getParent());
  78. mkdirsOrThrow(dir);
  79. if (zipEntry.isDirectory()) {
  80. mkdirsOrThrow(file);
  81. } else {
  82. byte[] buffer = new byte[1024];
  83. int length;
  84. try (FileOutputStream fos = new FileOutputStream(file)) {
  85. while ((length = zipInputStream.read(buffer)) >= 0) {
  86. fos.write(buffer, 0, length);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. private static void mkdirsOrThrow(File dir) throws IOException {
  94. if (!dir.exists() && !dir.mkdirs()) {
  95. throw new IOException("Failed to create directory " + dir);
  96. }
  97. }
  98. }