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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2012 Decebal Suiu
  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. public void extract() throws IOException {
  56. log.debug("Extract content of '{}' to '{}'", source, destination);
  57. // delete destination file if exists
  58. if (destination.exists() && destination.isDirectory()) {
  59. FileUtils.delete(destination.toPath());
  60. }
  61. try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(source))) {
  62. ZipEntry zipEntry;
  63. while ((zipEntry = zipInputStream.getNextEntry()) != null) {
  64. try {
  65. File file = new File(destination, zipEntry.getName());
  66. // create intermediary directories - sometimes zip don't add them
  67. File dir = new File(file.getParent());
  68. dir.mkdirs();
  69. if (zipEntry.isDirectory()) {
  70. file.mkdirs();
  71. } else {
  72. byte[] buffer = new byte[1024];
  73. int length;
  74. try (FileOutputStream fos = new FileOutputStream(file)) {
  75. while ((length = zipInputStream.read(buffer)) >= 0) {
  76. fos.write(buffer, 0, length);
  77. }
  78. }
  79. }
  80. } catch (FileNotFoundException e) {
  81. log.error("File '{}' not found", zipEntry.getName());
  82. }
  83. }
  84. }
  85. }
  86. }