001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * OutputZipper.java
019: * Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.experiment;
024:
025: import java.io.DataOutputStream;
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.util.zip.GZIPOutputStream;
029: import java.util.zip.ZipEntry;
030: import java.util.zip.ZipOutputStream;
031:
032: /**
033: * OutputZipper writes output to either gzipped files or to a
034: * multi entry zip file. If the destination file is a directory
035: * each output string will be written to an individually named
036: * gzip file. If the destination file is a file, then each
037: * output string is appended as a named entry to the zip file until
038: * finished() is called to close the file.
039: *
040: * @author Mark Hall (mhall@cs.waikato.ac.nz)
041: * @version $Revision: 1.7 $
042: */
043: public class OutputZipper {
044:
045: File m_destination;
046: DataOutputStream m_zipOut = null;
047: ZipOutputStream m_zs = null;
048:
049: /**
050: * Constructor.
051: *
052: * @param destination a destination file or directory
053: * @throws Exception if something goes wrong.
054: */
055: public OutputZipper(File destination) throws Exception {
056:
057: m_destination = destination;
058:
059: // if a directory is specified then use gzip format, otherwise
060: // use zip
061: if (!m_destination.isDirectory()) {
062: m_zs = new ZipOutputStream(new FileOutputStream(
063: m_destination));
064: m_zipOut = new DataOutputStream(m_zs);
065: }
066: }
067:
068: /**
069: * Saves a string to either an individual gzipped file or as
070: * an entry in a zip file.
071: *
072: * @param outString the output string to save
073: * @param name the name of the file/entry to save it to
074: * @throws Exception if something goes wrong
075: */
076: public void zipit(String outString, String name) throws Exception {
077: File saveFile;
078: ZipEntry ze;
079:
080: if (m_zipOut == null) {
081: saveFile = new File(m_destination, name + ".gz");
082: DataOutputStream dout = new DataOutputStream(
083: new GZIPOutputStream(new FileOutputStream(saveFile)));
084:
085: dout.writeBytes(outString);
086: dout.close();
087: } else {
088: ze = new ZipEntry(name);
089: m_zs.putNextEntry(ze);
090: m_zipOut.writeBytes(outString);
091: m_zs.closeEntry();
092: }
093: }
094:
095: /**
096: * Closes the zip file.
097: *
098: * @throws Exception if something goes wrong
099: */
100: public void finished() throws Exception {
101: if (m_zipOut != null) {
102: m_zipOut.close();
103: }
104: }
105:
106: /**
107: * Main method for testing this class
108: */
109: public static void main(String[] args) {
110:
111: try {
112: File testF = new File(new File(System
113: .getProperty("user.dir")), "testOut.zip");
114: OutputZipper oz = new OutputZipper(testF);
115:
116: /* OutputZipper oz = new OutputZipper(
117: new File(System.getProperty("user.dir"))); */
118: oz.zipit("Here is some test text to be zipped", "testzip");
119: oz.zipit("Here is a second entry to be zipped", "testzip2");
120: oz.finished();
121: } catch (Exception ex) {
122: ex.printStackTrace();
123: System.err.println(ex.getMessage());
124: }
125: }
126: }
|