source: branches/MetisMQI/src/main/java/weka/experiment/OutputZipper.java

Last change on this file was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 3.9 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    OutputZipper.java
19 *    Copyright (C) 2000 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.experiment;
25
26import weka.core.RevisionHandler;
27import weka.core.RevisionUtils;
28
29import java.io.DataOutputStream;
30import java.io.File;
31import java.io.FileOutputStream;
32import java.util.zip.GZIPOutputStream;
33import java.util.zip.ZipEntry;
34import java.util.zip.ZipOutputStream;
35
36/**
37 * OutputZipper writes output to either gzipped files or to a
38 * multi entry zip file. If the destination file is a directory
39 * each output string will be written to an individually named
40 * gzip file. If the destination file is a file, then each
41 * output string is appended as a named entry to the zip file until
42 * finished() is called to close the file.
43 *
44 * @author Mark Hall (mhall@cs.waikato.ac.nz)
45 * @version $Revision: 1.8 $
46 */
47public class OutputZipper
48  implements RevisionHandler {
49 
50  File m_destination;
51  DataOutputStream m_zipOut = null;
52  ZipOutputStream m_zs = null;
53
54  /**
55   * Constructor.
56   *
57   * @param destination a destination file or directory
58   * @throws Exception if something goes wrong.
59   */
60  public OutputZipper(File destination) throws Exception { 
61
62    m_destination = destination;
63
64    // if a directory is specified then use gzip format, otherwise
65    // use zip
66    if (!m_destination.isDirectory()) {
67      m_zs = new ZipOutputStream(new FileOutputStream(m_destination));
68      m_zipOut = new DataOutputStream(m_zs);
69    }   
70  }
71
72  /**
73   * Saves a string to either an individual gzipped file or as
74   * an entry in a zip file.
75   *
76   * @param outString the output string to save
77   * @param name the name of the file/entry to save it to
78   * @throws Exception if something goes wrong
79   */
80  public void zipit(String outString, String name) throws Exception {
81    File saveFile;
82    ZipEntry ze;
83   
84    if (m_zipOut == null) {
85      saveFile = new File(m_destination, name+".gz");
86      DataOutputStream dout = 
87        new DataOutputStream(new GZIPOutputStream(
88                             new FileOutputStream(saveFile)));
89     
90      dout.writeBytes(outString);
91      dout.close();
92    } else {
93      ze = new ZipEntry(name);
94      m_zs.putNextEntry(ze);
95      m_zipOut.writeBytes(outString);
96      m_zs.closeEntry();
97    }
98  }
99
100  /**
101   * Closes the zip file.
102   *
103   * @throws Exception if something goes wrong
104   */
105  public void finished() throws Exception {
106    if (m_zipOut != null) {
107      m_zipOut.close();
108    }
109  }
110 
111  /**
112   * Returns the revision string.
113   *
114   * @return            the revision
115   */
116  public String getRevision() {
117    return RevisionUtils.extract("$Revision: 1.8 $");
118  }
119
120  /**
121   * Main method for testing this class
122   */
123  public static void main(String [] args) {
124   
125    try {
126      File testF = new File(new File(System.getProperty("user.dir")), 
127                            "testOut.zip");
128      OutputZipper oz = new OutputZipper(testF);
129     
130      /*      OutputZipper oz = new OutputZipper(
131              new File(System.getProperty("user.dir"))); */
132      oz.zipit("Here is some test text to be zipped","testzip");
133      oz.zipit("Here is a second entry to be zipped","testzip2");
134      oz.finished();
135    } catch (Exception ex) {
136      ex.printStackTrace();
137      System.err.println(ex.getMessage());
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.