1 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 ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.util.zip.ZipOutputStream ; 25 import java.util.zip.ZipEntry ; 26 27 public class ZipTask implements PublicationProcessTask { 28 private static final int BUFFER_SIZE = 32768; 29 30 public void run(PublicationContext context) throws Exception { 31 BookInstance bookInstance = context.getBookInstance(); 32 String zipName = bookInstance.getName() + "-" + context.getPublicationOutputName(); 35 String zipFileName = "output/" + zipName + ".zip"; 36 37 String publicationOutputPath = BookInstanceLayout.getPublicationOutputPath(context.getPublicationOutputName()); 38 String [] paths = context.getBookInstance().getDescendantPaths(publicationOutputPath + "output"); 39 String prefix = publicationOutputPath + "output"; 40 41 OutputStream os = null; 42 InputStream is = null; 43 try { 44 os = context.getBookInstance().getResourceOutputStream(publicationOutputPath + zipFileName); 45 ZipOutputStream zos = new ZipOutputStream (os); 46 for (int i = 0; i < paths.length; i++) { 47 String name = zipName + paths[i].substring(prefix.length()); 48 ZipEntry zipEntry = new ZipEntry (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 is, OutputStream os) throws IOException { 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 |