2.java 11 KB

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