KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > packaging > ZipFactory


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Christophe Contreras
23 Contributor(s): ___________________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.packaging;
28
29 import java.io.*;
30 import java.util.zip.*;
31
32 /**
33  * This is the factory that can build zip archives from files added
34  *
35  * @author <a HREF="mailto:Christophe.Contreras@lifl.fr">Christophe Contreras</a>
36  *
37  */

38 public class ZipFactory
39 {
40     // ==================================================================
41
//
42
// Internal state.
43
//
44
// ==================================================================
45

46     /** the files to zip list */
47     private java.util.List JavaDoc files_to_zip_;
48
49     /** the name of archive */
50     private String JavaDoc zipname_;
51
52     // ==================================================================
53
//
54
// Constructors.
55
//
56
// ==================================================================
57

58     /**
59      * The default constructor.
60      */

61     public
62     ZipFactory()
63     {
64         // inits the files list
65
files_to_zip_ = new java.util.LinkedList JavaDoc();
66
67     }
68
69     // ==================================================================
70
//
71
// Internal methods.
72
//
73
// ==================================================================
74

75     /**
76      * compress given file with intern dir name into the zip stream
77      *
78      * @param File inputfile the file to zip
79      * @param ZipOutputStream zos the stream
80      */

81     private void
82     compressFile(File inputfile, ZipOutputStream zos)
83         throws Exception JavaDoc
84     {
85         byte[] buff = new byte[2048];
86         FileInputStream fis = new FileInputStream(inputfile);
87
88         int ret = 0;
89
90         zos.putNextEntry(new ZipEntry(inputfile.getPath()));
91         while (ret!=-1) {
92             ret = fis.read(buff, 0, 2048);
93             if (ret!=-1) {
94                 zos.write(buff, 0, ret);
95             }
96         }
97
98         zos.closeEntry();
99         fis.close();
100     }
101
102     // ==================================================================
103
//
104
// Public methods.
105
//
106
// ==================================================================
107

108     /**
109      * add method to give the factory a new file to archive
110      *
111      * @param String filename the filename that will be archived
112      */

113     public void
114     addFileToZip(String JavaDoc filename)
115     {
116         if (! files_to_zip_.contains(filename))
117         {
118             files_to_zip_.add(filename);
119         }
120     }
121
122     /**
123      * the set method for the zip archive filename
124      *
125      * @param String name the file name to use
126      */

127     public void
128     setZipName(String JavaDoc name)
129     {
130         this.zipname_ = name;
131     }
132
133     /**
134      * build method for the generation of the zip file
135      *
136      */

137     public void
138     buildZip()
139         throws Exception JavaDoc
140     {
141         // get each file added to the private list
142
String JavaDoc[] files
143             = (String JavaDoc[]) files_to_zip_.toArray(new String JavaDoc[0]);
144
145         // inits the output stream
146
FileOutputStream fos = new FileOutputStream(zipname_);
147         ZipOutputStream zos = new ZipOutputStream(fos);
148
149         // adds the file to the stream
150
for (int i=0;i<files.length;i++)
151         {
152
153             // tests if the file can be found
154
java.io.File JavaDoc file_to_add
155              = new java.io.File JavaDoc (
156                 files[i]
157                 );
158
159             if ( file_to_add.exists() )
160             {
161                 System.err.println("Adding file: "+file_to_add.getPath()+" to zip");
162                 compressFile(
163                     file_to_add,
164                     zos
165                     );
166             } else {
167                 System.err.println(
168                     files[i]
169                     +" can not be found -> skipping.."
170                     );
171             }
172         }
173
174         zos.close();
175     }
176 }
177
178
Popular Tags