KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > publicationprocess > ZipTask


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.publicationprocess;
17
18 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout;
19 import org.outerj.daisy.books.store.BookInstance;
20
21 import java.io.OutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.zip.ZipOutputStream JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26
27 public class ZipTask implements PublicationProcessTask {
28     private static final int BUFFER_SIZE = 32768;
29
30     public void run(PublicationContext context) throws Exception JavaDoc {
31         BookInstance bookInstance = context.getBookInstance();
32         // file name is fixed -- if this would ever change also check PublicationType where
33
// the name gets registered with the bookInstance
34
String JavaDoc zipName = bookInstance.getName() + "-" + context.getPublicationOutputName();
35         String JavaDoc zipFileName = "output/" + zipName + ".zip";
36
37         String JavaDoc publicationOutputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName());
38         String JavaDoc[] paths = context.getBookInstance().getDescendantPaths(publicationOutputPath + "output");
39         String JavaDoc prefix = publicationOutputPath + "output";
40
41         OutputStream JavaDoc os = null;
42         InputStream JavaDoc is = null;
43         try {
44             os = context.getBookInstance().getResourceOutputStream(publicationOutputPath + zipFileName);
45             ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(os);
46             for (int i = 0; i < paths.length; i++) {
47                 String JavaDoc name = zipName + paths[i].substring(prefix.length());
48                 ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(name);
49                 zos.putNextEntry(zipEntry);
50                 is = bookInstance.getResource(paths[i]);
51                 copy(is, zos);
52                 is.close();
53                 zos.closeEntry();
54             }
55             zos.finish();
56             zos.flush();
57         } finally {
58             if (is != null)
59                 is.close();
60             if (os != null)
61                 os.close();
62         }
63     }
64
65     private void copy(InputStream JavaDoc is, OutputStream JavaDoc os) throws IOException JavaDoc {
66         byte[] buffer = new byte[BUFFER_SIZE];
67         int read;
68         while ((read = is.read(buffer)) != -1) {
69             os.write(buffer, 0, read);
70         }
71     }
72 }
73
Popular Tags