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

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