2.java 11 KB

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