PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/files/UnZip.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 189 lines | 111 code | 15 blank | 63 comment | 20 complexity | 042766e6864c09d37f0540230e1c963c MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. *
  3. *
  4. */
  5. package mpv5.utils.files;
  6. /**
  7. *
  8. * @author
  9. */
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.util.Enumeration;
  15. import java.util.SortedSet;
  16. import java.util.TreeSet;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipFile;
  19. import mpv5.logging.Log;
  20. /**
  21. * UnZip -- print or unzip a JAR or PKZIP file using java.util.zip. Command-line
  22. * version: extracts files.
  23. *
  24. * @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07
  25. * 17:40:35 ian Exp $
  26. */
  27. public class UnZip {
  28. /** Constants for mode listing or mode extracting. */
  29. public static final int LIST = 0, EXTRACT = 1;
  30. /** Whether we are extracting or just printing TOC */
  31. protected int mode = LIST;
  32. /** The ZipFile that is used to read an archive */
  33. private ZipFile zippy;
  34. /** The buffer for reading/writing the ZipFile data */
  35. protected byte[] b;
  36. private String path;
  37. /**
  38. * Construct an UnZipper, process each .ZIP file from
  39. * zipFiles through that object.
  40. * @param zipfiles
  41. * @param toDir
  42. */
  43. public static void deflate(String zipfiles, String toDir) {
  44. File fil = new File(toDir);
  45. if (fil.isDirectory()) {
  46. fil.mkdirs();
  47. } else {
  48. fil.getParentFile().mkdirs();
  49. }
  50. UnZip u = new UnZip();
  51. u.setPath(toDir);
  52. u.setMode(EXTRACT);
  53. String candidate = zipfiles;
  54. Log.Debug(UnZip.class, "Trying path " + candidate);
  55. if (candidate.endsWith(".zip") || candidate.endsWith(".jar")) {
  56. u.unZip(candidate);
  57. } else {
  58. Log.Debug(UnZip.class, "Not a zip file? " + candidate);
  59. }
  60. }
  61. /** Construct an UnZip object. Just allocate the buffer */
  62. UnZip() {
  63. b = new byte[8092];
  64. }
  65. /** Set the Mode (list, extract).
  66. * @param m
  67. */
  68. protected void setMode(int m) {
  69. if (m == LIST || m == EXTRACT) {
  70. mode = m;
  71. }
  72. }
  73. /** Cache of paths we've mkdir()ed. */
  74. protected SortedSet dirsMade;
  75. /** For a given Zip file, process each entry.
  76. * @param fileName
  77. */
  78. public void unZip(String fileName) {
  79. dirsMade = new TreeSet();
  80. try {
  81. zippy = new ZipFile(fileName);
  82. Enumeration all = getZippy().entries();
  83. while (all.hasMoreElements()) {
  84. getFile((ZipEntry) all.nextElement());
  85. }
  86. } catch (IOException err) {
  87. Log.Debug(UnZip.class, "IO Error: " + err);
  88. return;
  89. }
  90. }
  91. protected boolean warnedMkDir = false;
  92. /**
  93. * Process one file from the zip, given its name. Either print the name, or
  94. * create the file on disk.
  95. * @param e
  96. * @throws java.io.IOException
  97. */
  98. @SuppressWarnings("unchecked")
  99. protected void getFile(ZipEntry e) throws IOException {
  100. String zipName = getPath() + File.separator + e.getName();
  101. switch (mode) {
  102. case EXTRACT:
  103. // if (zipName.startsWith("/")) {
  104. // if (!warnedMkDir) {
  105. // System.out.println("Ignoring absolute paths");
  106. // }
  107. // warnedMkDir = true;
  108. // zipName = zipName.substring(1);
  109. // }
  110. // if a directory, just return. We mkdir for every file,
  111. // since some widely-used Zip creators don't put out
  112. // any directory entries, or put them in the wrong place.
  113. if (zipName.endsWith("/")) {
  114. return;
  115. }
  116. // Else must be a file; open the file for output
  117. // Get the directory part.
  118. int ix = zipName.lastIndexOf('/');
  119. if (ix > 0) {
  120. String dirName = zipName.substring(0, ix);
  121. if (!dirsMade.contains(dirName)) {
  122. File d = new File(dirName);
  123. // If it already exists as a dir, don't do anything
  124. if (!(d.exists() && d.isDirectory())) {
  125. // Try to create the directory, warn if it fails
  126. Log.Debug(UnZip.class, "Creating Directory: " + dirName);
  127. if (!d.mkdirs()) {
  128. Log.Debug(UnZip.class, "Warning: unable to mkdir " + dirName);
  129. }
  130. dirsMade.add(dirName);
  131. }
  132. }
  133. }
  134. Log.Debug(UnZip.class, "Creating " + zipName);
  135. FileOutputStream os = new FileOutputStream(zipName);
  136. InputStream is = getZippy().getInputStream(e);
  137. int n = 0;
  138. while ((n = is.read(b)) > 0) {
  139. os.write(b, 0, n);
  140. }
  141. is.close();
  142. os.close();
  143. break;
  144. case LIST:
  145. // Not extracting, just list
  146. if (e.isDirectory()) {
  147. Log.Debug(UnZip.class, "Directory " + zipName);
  148. } else {
  149. Log.Debug(UnZip.class, "File " + zipName);
  150. }
  151. break;
  152. default:
  153. throw new IllegalStateException("mode value (" + mode + ") bad");
  154. }
  155. }
  156. /**
  157. * @return the path
  158. */
  159. public String getPath() {
  160. return path;
  161. }
  162. /**
  163. * @param path the path to set
  164. */
  165. public void setPath(String path) {
  166. this.path = path;
  167. }
  168. /**
  169. * @return the zippy
  170. */
  171. public ZipFile getZippy() {
  172. return zippy;
  173. }
  174. }