From: <pat...@us...> - 2011-02-18 03:30:47
|
Revision: 1219 http://cishell.svn.sourceforge.net/cishell/?rev=1219&view=rev Author: pataphil Date: 2011-02-18 03:30:41 +0000 (Fri, 18 Feb 2011) Log Message: ----------- * Added ZipUtilities.zipFilesWithNames() Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java 2011-02-18 03:29:48 UTC (rev 1218) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java 2011-02-18 03:30:41 UTC (rev 1219) @@ -23,7 +23,7 @@ throws ZipIOException { try { for (File file : files) { - writeFileToZipFile(file, zipOut); + writeFileToZipFile(file, file.getName(), zipOut); } zipOut.close(); @@ -44,13 +44,38 @@ } } - public static void writeFileToZipFile(File file, ZipOutputStream zipOut) + public static void zipFilesWithNames( + Map<File, String> fileToZippedName, ZipOutputStream zipOut) throws ZipIOException { + try { + for (File file : fileToZippedName.keySet()) { + writeFileToZipFile(file, fileToZippedName.get(file), zipOut); + } + + zipOut.close(); + } catch (IOException e) { + throw new ZipIOException(e.getMessage(), e); + } + } + + public static void zipFilesWithNames( + Map<File, String> fileToZippedName, File targetZipFile) throws ZipIOException { + try { + zipFilesWithNames( + fileToZippedName, + new ZipOutputStream( + new BufferedOutputStream(new FileOutputStream(targetZipFile)))); + } catch (FileNotFoundException e) { + throw new ZipIOException(e.getMessage(), e); + } + } + + public static void writeFileToZipFile(File file, String zippedName, ZipOutputStream zipOut) throws ZipIOException { try { byte data[] = new byte[BUFFER_SIZE]; BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE); - ZipEntry entry = new ZipEntry(file.getName()); + ZipEntry entry = new ZipEntry(zippedName); zipOut.putNextEntry(entry); int count; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pat...@us...> - 2011-03-01 20:14:36
|
Revision: 1232 http://cishell.svn.sourceforge.net/cishell/?rev=1232&view=rev Author: pataphil Date: 2011-03-01 20:14:29 +0000 (Tue, 01 Mar 2011) Log Message: ----------- * Cleaned up ZipUtilities. * Reviewed by Joseph. Modified Paths: -------------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java 2011-03-01 20:10:28 UTC (rev 1231) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/ZipUtilities.java 2011-03-01 20:14:29 UTC (rev 1232) @@ -17,6 +17,9 @@ import java.util.zip.ZipOutputStream; public class ZipUtilities { + /* 2048, as gotten from: + * http://java.sun.com/developer/technicalArticles/Programming/compression/ + */ static final int BUFFER_SIZE = 2048; public static void zipFiles(Collection<File> files, ZipOutputStream zipOut) @@ -29,7 +32,14 @@ zipOut.close(); } catch (IOException e) { throw new ZipIOException(e.getMessage(), e); + } finally { + try { + zipOut.close(); + } catch (IOException e) { + throw new ZipIOException(e.getMessage(), e); + } } + } public static void zipFiles(Collection<File> files, File targetZipFile) @@ -54,6 +64,12 @@ zipOut.close(); } catch (IOException e) { throw new ZipIOException(e.getMessage(), e); + } finally { + try { + zipOut.close(); + } catch (IOException e) { + throw new ZipIOException(e.getMessage(), e); + } } } @@ -69,6 +85,10 @@ } } + /* Again, refer to: + * http://java.sun.com/developer/technicalArticles/Programming/compression/ + * for a reference on where this solution came from. + */ public static void writeFileToZipFile(File file, String zippedName, ZipOutputStream zipOut) throws ZipIOException { try { @@ -98,10 +118,17 @@ BufferedInputStream reader = new BufferedInputStream(zipFile.getInputStream(entry)); String fileName = new File(entry.getName()).getName(); File outputFile = - FileUtilities.createTemporaryFileInDefaultTemporaryDirectory(fileName, ""); + FileUtilities.createTemporaryFileInDefaultTemporaryDirectory(fileName, "tmp"); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile), BUFFER_SIZE); + /* TODO Could we have: + * writeFileToZipFile(BufferedInputStream in, String zippedName, ZipOutputStream zipOut) + * and then have both: + * writeFileToZipFile(File file, String zippedName, ZipOutputStream zipOut) + * and this method use that as a common utility? + * (Maybe eventually, if someone wants to do this.) + */ byte readBytes[] = new byte[BUFFER_SIZE]; int readByteCount; @@ -142,27 +169,4 @@ return CollectionUtilities.collectionEnumerationElements( entries, new ArrayList<ZipEntry>()); } - - public static void main(String[] args) { - String filePath = - "C:\\Documents and Settings\\pataphil\\Desktop\\org.cishell.utility.swt.zip"; - - try { - ZipFile zipFile = new ZipFile(filePath); - Map<String, ZipEntry> entriesByName = mapFileNamesToEntries(zipFile, false); - - int count = 0; - for (String key : entriesByName.keySet()) { - System.err.println(key + ": " + entriesByName.get(key)); - count++; - - if (count == 2) { - readFileFromZipFile(entriesByName.get(key), zipFile); - } - } - } catch (IOException e) { - System.err.println("Exception: " + e.getMessage()); - e.printStackTrace(); - } - } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |