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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.util.zip.ZipEntry;
  23. import java.util.zip.ZipInputStream;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. /**
  27. * This class extracts the content of the plugin zip into a directory.
  28. * It's a class for only the internal use.
  29. *
  30. * @author Decebal Suiu
  31. */
  32. public class Unzip {
  33. private static final Logger log = LoggerFactory.getLogger(Unzip.class);
  34. /**
  35. * Holds the destination directory.
  36. * File will be unzipped into the destination directory.
  37. */
  38. private File destination;
  39. /**
  40. * Holds path to zip file.
  41. */
  42. private File source;
  43. public Unzip() {
  44. }
  45. public Unzip(File source, File destination) {
  46. this.source = source;
  47. this.destination = destination;
  48. }
  49. public void setSource(File source) {
  50. this.source = source;
  51. }
  52. public void setDestination(File destination) {
  53. this.destination = destination;
  54. }
  55. /**
  56. * Extract the content of zip file ({@code source}) to destination directory.
  57. * If destination directory already exists it will be deleted before.
  58. */
  59. public void extract() throws IOException {
  60. log.debug("Extract content of '{}' to '{}'", source, destination);
  61. // delete destination directory if exists
  62. if (destination.exists() && destination.isDirectory()) {
  63. FileUtils.delete(destination.toPath());
  64. }
  65. try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
  66. ZipEntry zipEntry;
  67. while ((zipEntry = zipInputStream.getNextEntry()) != null) {
  68. File file = new File(destination, zipEntry.getName());
  69. // create intermediary directories - sometimes zip don't add them
  70. File dir = new File(file.getParent());
  71. mkdirsOrThrow(dir);
  72. if (zipEntry.isDirectory()) {
  73. mkdirsOrThrow(file);
  74. } else {
  75. byte[] buffer = new byte[1024];
  76. int length;
  77. try (FileOutputStream fos = new FileOutputStream(file)) {
  78. while ((length = zipInputStream.read(buffer)) >= 0) {
  79. fos.write(buffer, 0, length);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. private static void mkdirsOrThrow(File dir) throws IOException {
  87. if (!dir.exists() && !dir.mkdirs()) {
  88. throw new IOException("Failed to create directory " + dir);
  89. }
  90. }
  91. }