@Test public void shouldNotFailWhenDestinationDirectoryNameIsLess3Chars() throws Exception { final File parent = new File( System.getProperty( "java.io.tmpdir" ) ); final File destination = new File( parent, "a" ); destination.mkdir(); destination.deleteOnExit(); final InputStream zipStream = TestUtilities.sampleDataserviceExample(); FileUtils.zipExtract( zipStream, destination ); }
public static void zipExtract(InputStream fileStream, File destDirectory) throws Exception { ArgCheck.isNotNull(fileStream, "file stream"); ArgCheck.isNotNull(destDirectory, "destination directory"); ArgCheck.isTrue(destDirectory.isDirectory(), "destination is not a directory"); File tmpFile = null; ZipFile zipFile = null; try { String prefix = destDirectory.getName(); while ( prefix.length() < 3 ) { prefix += '_'; } tmpFile = File.createTempFile(prefix, ZIP_SUFFIX); write(fileStream, tmpFile); zipFile = new ZipFile(tmpFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!entries.hasMoreElements()) return; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream zipStream = null; try { String name = entry.getName(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; zipStream = zipFile.getInputStream(entry); File newFile = new File( destDirectory, name ); if (entry.isDirectory()) { Files.createDirectories(newFile.toPath()); continue; } else { Files.createDirectories(newFile.getParentFile().toPath()); } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zipStream.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } finally { if (zipStream != null) zipStream.close(); } } } finally { if (zipFile != null) zipFile.close(); if (tmpFile != null) tmpFile.delete(); } }
FileUtils implements StringConstants { public static void zipExtract(InputStream fileStream, File destDirectory) throws Exception { ArgCheck.isNotNull(fileStream, "file stream"); ArgCheck.isNotNull(destDirectory, "destination directory"); ArgCheck.isTrue(destDirectory.isDirectory(), "destination is not a directory"); File tmpFile = null; ZipFile zipFile = null; try { String prefix = destDirectory.getName(); while ( prefix.length() < 3 ) { prefix += '_'; } tmpFile = File.createTempFile(prefix, ZIP_SUFFIX); write(fileStream, tmpFile); zipFile = new ZipFile(tmpFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!entries.hasMoreElements()) return; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream zipStream = null; try { String name = entry.getName(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; zipStream = zipFile.getInputStream(entry); File newFile = new File( destDirectory, name ); if (entry.isDirectory()) { Files.createDirectories(newFile.toPath()); continue; } else { Files.createDirectories(newFile.getParentFile().toPath()); } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zipStream.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } finally { if (zipStream != null) zipStream.close(); } } } finally { if (zipFile != null) zipFile.close(); if (tmpFile != null) tmpFile.delete(); } } }
FileUtils implements StringConstants { public static void zipExtract(InputStream fileStream, File destDirectory) throws Exception { ArgCheck.isNotNull(fileStream, "file stream"); ArgCheck.isNotNull(destDirectory, "destination directory"); ArgCheck.isTrue(destDirectory.isDirectory(), "destination is not a directory"); File tmpFile = null; ZipFile zipFile = null; try { String prefix = destDirectory.getName(); while ( prefix.length() < 3 ) { prefix += '_'; } tmpFile = File.createTempFile(prefix, ZIP_SUFFIX); write(fileStream, tmpFile); zipFile = new ZipFile(tmpFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!entries.hasMoreElements()) return; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream zipStream = null; try { String name = entry.getName(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; zipStream = zipFile.getInputStream(entry); File newFile = new File( destDirectory, name ); if (entry.isDirectory()) { Files.createDirectories(newFile.toPath()); continue; } else { Files.createDirectories(newFile.getParentFile().toPath()); } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zipStream.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } finally { if (zipStream != null) zipStream.close(); } } } finally { if (zipFile != null) zipFile.close(); if (tmpFile != null) tmpFile.delete(); } } private  FileUtils(); }
FileUtils implements StringConstants { public static void zipExtract(InputStream fileStream, File destDirectory) throws Exception { ArgCheck.isNotNull(fileStream, "file stream"); ArgCheck.isNotNull(destDirectory, "destination directory"); ArgCheck.isTrue(destDirectory.isDirectory(), "destination is not a directory"); File tmpFile = null; ZipFile zipFile = null; try { String prefix = destDirectory.getName(); while ( prefix.length() < 3 ) { prefix += '_'; } tmpFile = File.createTempFile(prefix, ZIP_SUFFIX); write(fileStream, tmpFile); zipFile = new ZipFile(tmpFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!entries.hasMoreElements()) return; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream zipStream = null; try { String name = entry.getName(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; zipStream = zipFile.getInputStream(entry); File newFile = new File( destDirectory, name ); if (entry.isDirectory()) { Files.createDirectories(newFile.toPath()); continue; } else { Files.createDirectories(newFile.getParentFile().toPath()); } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zipStream.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } finally { if (zipStream != null) zipStream.close(); } } } finally { if (zipFile != null) zipFile.close(); if (tmpFile != null) tmpFile.delete(); } } private  FileUtils(); final static ArrayList<File> getFilesForPattern(String pathToScan, String startWith, String endsWith); static String readSafe(File file); static String read(Reader reader); static void removeChildrenRecursively(final File directory); static void removeDirectoryAndChildren(final File directory); static byte[] write(final InputStream is); static void write(final InputStream is, final File f); static void write(final InputStream is, final File f, final int bufferSize); static void write(final InputStream is, final String fileName); static void write(byte[] data, String fileName); static void write(byte[] data, File file); static String streamToString(InputStream inStream); static byte[] streamToByteArray(InputStream inStream); static String tempDirectory(); static Document createDocument(String xml); static void zipExtract(InputStream fileStream, File destDirectory); static File zipFromDirectory(File srcDirectory, File zipFile); }
FileUtils implements StringConstants { public static void zipExtract(InputStream fileStream, File destDirectory) throws Exception { ArgCheck.isNotNull(fileStream, "file stream"); ArgCheck.isNotNull(destDirectory, "destination directory"); ArgCheck.isTrue(destDirectory.isDirectory(), "destination is not a directory"); File tmpFile = null; ZipFile zipFile = null; try { String prefix = destDirectory.getName(); while ( prefix.length() < 3 ) { prefix += '_'; } tmpFile = File.createTempFile(prefix, ZIP_SUFFIX); write(fileStream, tmpFile); zipFile = new ZipFile(tmpFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); if (!entries.hasMoreElements()) return; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); InputStream zipStream = null; try { String name = entry.getName(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; zipStream = zipFile.getInputStream(entry); File newFile = new File( destDirectory, name ); if (entry.isDirectory()) { Files.createDirectories(newFile.toPath()); continue; } else { Files.createDirectories(newFile.getParentFile().toPath()); } FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zipStream.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } finally { if (zipStream != null) zipStream.close(); } } } finally { if (zipFile != null) zipFile.close(); if (tmpFile != null) tmpFile.delete(); } } private  FileUtils(); final static ArrayList<File> getFilesForPattern(String pathToScan, String startWith, String endsWith); static String readSafe(File file); static String read(Reader reader); static void removeChildrenRecursively(final File directory); static void removeDirectoryAndChildren(final File directory); static byte[] write(final InputStream is); static void write(final InputStream is, final File f); static void write(final InputStream is, final File f, final int bufferSize); static void write(final InputStream is, final String fileName); static void write(byte[] data, String fileName); static void write(byte[] data, File file); static String streamToString(InputStream inStream); static byte[] streamToByteArray(InputStream inStream); static String tempDirectory(); static Document createDocument(String xml); static void zipExtract(InputStream fileStream, File destDirectory); static File zipFromDirectory(File srcDirectory, File zipFile); static int DEFAULT_BUFFER_SIZE; }