1.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * Copyright (C) 2012 ZeroTurnaround LLC <support@zeroturnaround.com>
  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. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.util.zip.ZipEntry;
  21. import java.util.zip.ZipOutputStream;
  22. import junit.framework.TestCase;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.io.IOUtils;
  25. import org.zeroturnaround.zip.ZipEntrySource;
  26. import org.zeroturnaround.zip.ZipException;
  27. import org.zeroturnaround.zip.ZipUtil;
  28. public class ZipUtilTest extends TestCase {
  29. public void testUnpackEntryFromFile() throws IOException {
  30. final String name = "foo";
  31. final byte[] contents = "bar".getBytes();
  32. File file = File.createTempFile("temp", null);
  33. try {
  34. // Create the ZIP file
  35. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
  36. try {
  37. zos.putNextEntry(new ZipEntry(name));
  38. zos.write(contents);
  39. zos.closeEntry();
  40. }
  41. finally {
  42. IOUtils.closeQuietly(zos);
  43. }
  44. // Test the ZipUtil
  45. byte[] actual = ZipUtil.unpackEntry(file, name);
  46. assertNotNull(actual);
  47. assertEquals(new String(contents), new String(actual));
  48. }
  49. // 1
  50. // 2
  51. // 3
  52. finally {
  53. FileUtils.deleteQuietly(file);
  54. }
  55. }
  56. public void testUnpackEntryFromStream() throws IOException {
  57. final String name = "foo";
  58. final byte[] contents = "bar".getBytes();
  59. File file = File.createTempFile("temp", null);
  60. try {
  61. // Create the ZIP file
  62. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
  63. try {
  64. zos.putNextEntry(new ZipEntry(name));
  65. zos.write(contents);
  66. zos.closeEntry();
  67. }
  68. finally {
  69. IOUtils.closeQuietly(zos);
  70. }
  71. FileInputStream fis = new FileInputStream(file);
  72. // Test the ZipUtil
  73. byte[] actual = ZipUtil.unpackEntry(fis, name);
  74. assertNotNull(actual);
  75. assertEquals(new String(contents), new String(actual));
  76. }
  77. // 1
  78. // 2
  79. // 3
  80. finally {
  81. FileUtils.deleteQuietly(file);
  82. }
  83. }
  84. public void testDuplicateEntryAtAdd() throws IOException {
  85. File src = new File(getClass().getResource("duplicate.zip").getPath());
  86. File dest = File.createTempFile("temp", null);
  87. try {
  88. ZipUtil.addEntries(src, new ZipEntrySource[0], dest);
  89. }
  90. finally {
  91. FileUtils.deleteQuietly(dest);
  92. }
  93. }
  94. public void testDuplicateEntryAtReplace() throws IOException {
  95. File src = new File(getClass().getResource("duplicate.zip").getPath());
  96. File dest = File.createTempFile("temp", null);
  97. try {
  98. ZipUtil.replaceEntries(src, new ZipEntrySource[0], dest);
  99. }
  100. finally {
  101. FileUtils.deleteQuietly(dest);
  102. }
  103. }
  104. public void testDuplicateEntryAtAddOrReplace() throws IOException {
  105. File src = new File(getClass().getResource("duplicate.zip").getPath());
  106. File dest = File.createTempFile("temp", null);
  107. try {
  108. ZipUtil.addOrReplaceEntries(src, new ZipEntrySource[0], dest);
  109. }
  110. finally {
  111. FileUtils.deleteQuietly(dest);
  112. }
  113. }
  114. public void testUnexplode() throws IOException {
  115. File file = File.createTempFile("tempFile", null);
  116. File tmpDir = file.getParentFile();
  117. unexplodeWithException(file, "shouldn't be able to unexplode file that is not a directory");
  118. assertTrue("Should be able to delete tmp file", file.delete());
  119. unexplodeWithException(file, "shouldn't be able to unexplode file that doesn't exist");
  120. // create empty tmp dir with the same name as deleted file
  121. File dir = new File(tmpDir, file.getName());
  122. dir.deleteOnExit();
  123. assertTrue("Should be able to create directory with the same name as there was tmp file", dir.mkdir());
  124. unexplodeWithException(dir, "shouldn't be able to unexplode dir that doesn't contain any files");
  125. // unexplode should succeed with at least one file in directory
  126. File.createTempFile("temp", null, dir);
  127. ZipUtil.unexplode(dir);
  128. assertTrue("zip file should exist with the same name as the directory that was unexploded", dir.exists());
  129. assertTrue("unexploding input directory should have produced zip file with the same name", !dir.isDirectory());
  130. assertTrue("Should be able to delete zip that was created from directory", dir.delete());
  131. }
  132. public void testPackEntry() throws Exception {
  133. File fileToPack = new File(getClass().getResource("TestFile.txt").getPath());
  134. File dest = File.createTempFile("temp", null);
  135. ZipUtil.packEntry(fileToPack, dest);
  136. assertTrue(dest.exists());
  137. ZipUtil.explode(dest);
  138. assertTrue((new File(dest, "TestFile.txt")).exists());
  139. // if fails then maybe somebody changed the file contents and did not update
  140. // the test
  141. assertEquals(108, (new File(dest, "TestFile.txt")).length());
  142. }
  143. public void testPackEntries() throws Exception {
  144. File fileToPack = new File(getClass().getResource("TestFile.txt").getPath());
  145. File fileToPackII = new File(getClass().getResource("TestFile-II.txt").getPath());
  146. File dest = File.createTempFile("temp", null);
  147. ZipUtil.packEntries(new File[] { fileToPack, fileToPackII }, dest);
  148. assertTrue(dest.exists());
  149. ZipUtil.explode(dest);
  150. assertTrue((new File(dest, "TestFile.txt")).exists());
  151. assertTrue((new File(dest, "TestFile-II.txt")).exists());
  152. // if fails then maybe somebody changed the file contents and did not update
  153. // the test
  154. assertEquals(108, (new File(dest, "TestFile.txt")).length());
  155. assertEquals(103, (new File(dest, "TestFile-II.txt")).length());
  156. }
  157. public void testZipException() {
  158. boolean exceptionThrown = false;
  159. try {
  160. ZipUtil.pack(new File("nonExistent"), new File("weeheha"));
  161. }
  162. catch (ZipException e) {
  163. exceptionThrown = true;
  164. }
  165. assertTrue(exceptionThrown);
  166. }
  167. public void testPreserveRoot() throws Exception {
  168. File dest = File.createTempFile("temp", null);
  169. File parent = new File(getClass().getResource("TestFile.txt").getPath()).getParentFile();
  170. ZipUtil.pack(parent, dest, true);
  171. ZipUtil.explode(dest);
  172. assertTrue((new File(dest, parent.getName())).exists());
  173. }
  174. private void unexplodeWithException(File file, String message) {
  175. boolean ok = false;
  176. try {
  177. ZipUtil.unexplode(file);
  178. }
  179. catch (Exception e) {
  180. ok = true;
  181. }
  182. assertTrue(message, ok);
  183. }
  184. public void testArchiveEquals() {
  185. File src = new File(getClass().getResource("demo.zip").getPath());
  186. // byte-by-byte copy
  187. File src2 = new File(getClass().getResource("demo-copy.zip").getPath());
  188. assertTrue(ZipUtil.archiveEquals(src, src2));
  189. // entry by entry copy
  190. File src3 = new File(getClass().getResource("demo-copy-II.zip").getPath());
  191. assertTrue(ZipUtil.archiveEquals(src, src3));
  192. }
  193. public void testRepackArchive() throws IOException {
  194. File src = new File(getClass().getResource("demo.zip").getPath());
  195. File dest = File.createTempFile("temp", null);
  196. ZipUtil.repack(src, dest, 1);
  197. assertTrue(ZipUtil.archiveEquals(src, dest));
  198. }
  199. public void testContainsAnyEntry() throws IOException {
  200. File src = new File(getClass().getResource("demo.zip").getPath());
  201. boolean exists = ZipUtil.containsAnyEntry(src, new String[] { "foo.txt", "bar.txt" });
  202. assertTrue(exists);
  203. exists = ZipUtil.containsAnyEntry(src, new String[] { "foo.txt", "does-not-exist.txt" });
  204. assertTrue(exists);
  205. exists = ZipUtil.containsAnyEntry(src, new String[] { "does-not-exist-I.txt", "does-not-exist-II.txt" });
  206. assertFalse(exists);
  207. }
  208. public void testAddEntry() throws IOException {
  209. File src = new File(getClass().getResource("demo.zip").getPath());
  210. final String fileName = "TestFile.txt";
  211. assertFalse(ZipUtil.containsEntry(src, fileName));
  212. File newEntry = new File(getClass().getResource(fileName).getPath());
  213. File dest = File.createTempFile("temp.zip", null);
  214. ZipUtil.addEntry(src, fileName, newEntry, dest);
  215. assertTrue(ZipUtil.containsEntry(dest, fileName));
  216. }
  217. public void testRemoveEntry() throws IOException {
  218. File src = new File(getClass().getResource("demo.zip").getPath());
  219. File dest = File.createTempFile("temp", null);
  220. try {
  221. ZipUtil.removeEntry(src, "bar.txt", dest);
  222. assertTrue("Result zip misses entry 'foo.txt'", ZipUtil.containsEntry(dest, "foo.txt"));
  223. assertTrue("Result zip misses entry 'foo1.txt'", ZipUtil.containsEntry(dest, "foo1.txt"));
  224. assertTrue("Result zip misses entry 'foo2.txt'", ZipUtil.containsEntry(dest, "foo2.txt"));
  225. assertFalse("Result zip still contains 'bar.txt'", ZipUtil.containsEntry(dest, "bar.txt"));
  226. }
  227. finally {
  228. FileUtils.deleteQuietly(dest);
  229. }
  230. }
  231. public void testRemoveDirs() throws IOException {
  232. File src = new File(getClass().getResource("demo-dirs.zip").getPath());
  233. File dest = File.createTempFile("temp", null);
  234. try {
  235. ZipUtil.removeEntries(src, new String[] { "bar.txt", "a/b" }, dest);
  236. assertFalse("Result zip still contains 'bar.txt'", ZipUtil.containsEntry(dest, "bar.txt"));
  237. assertFalse("Result zip still contains dir 'a/b'", ZipUtil.containsEntry(dest, "a/b"));
  238. assertTrue("Result doesn't containt 'attic'", ZipUtil.containsEntry(dest, "attic/treasure.txt"));
  239. assertTrue("Entry whose prefix is dir name is removed too: 'b.txt'", ZipUtil.containsEntry(dest, "a/b.txt"));
  240. assertFalse("Entry in a removed dir is still there: 'a/b/c.txt'", ZipUtil.containsEntry(dest, "a/b/c.txt"));
  241. }
  242. finally {
  243. FileUtils.deleteQuietly(dest);
  244. }
  245. }
  246. }