test/java/util/zip/ZipFile/ShortRead.java

Print this page
rev 3516 : 7021582: convert jar/zip code and tests to use try-with-resources
Reviewed-by: XXX

*** 36,53 **** public static void main(String[] args) throws Exception { final File zFile = new File("abc.zip"); try { final String entryName = "abc"; final String data = "Data disponible"; ! final ZipOutputStream zos = ! new ZipOutputStream(new FileOutputStream(zFile)); zos.putNextEntry(new ZipEntry(entryName)); zos.write(data.getBytes("ASCII")); zos.closeEntry(); ! zos.close(); ! final ZipFile zipFile = new ZipFile(zFile); final ZipEntry zentry = zipFile.getEntry(entryName); final InputStream inputStream = zipFile.getInputStream(zentry); System.out.printf("size=%d csize=%d available=%d%n", zentry.getSize(), zentry.getCompressedSize(), --- 36,54 ---- public static void main(String[] args) throws Exception { final File zFile = new File("abc.zip"); try { final String entryName = "abc"; final String data = "Data disponible"; ! try (FileOutputStream fos = new FileOutputStream(zFile); ! ZipOutputStream zos = new ZipOutputStream(fos)) ! { zos.putNextEntry(new ZipEntry(entryName)); zos.write(data.getBytes("ASCII")); zos.closeEntry(); ! } ! try (ZipFile zipFile = new ZipFile(zFile)) { final ZipEntry zentry = zipFile.getEntry(entryName); final InputStream inputStream = zipFile.getInputStream(zentry); System.out.printf("size=%d csize=%d available=%d%n", zentry.getSize(), zentry.getCompressedSize(),
*** 55,64 **** byte[] buf = new byte[data.length()]; final int count = inputStream.read(buf); if (! new String(buf, "ASCII").equals(data) || count != data.length()) throw new Exception("short read?"); - zipFile.close(); } ! finally { zFile.delete(); } } } --- 56,66 ---- byte[] buf = new byte[data.length()]; final int count = inputStream.read(buf); if (! new String(buf, "ASCII").equals(data) || count != data.length()) throw new Exception("short read?"); } ! } finally { ! zFile.delete(); ! } } }