Source for org.jfree.repository.zipwriter.ZipRepository

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * ZipRepository.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository.zipwriter;
  32: 
  33: import java.io.OutputStream;
  34: import java.io.IOException;
  35: import java.io.InputStream;
  36: import java.util.zip.ZipOutputStream;
  37: import java.util.zip.ZipEntry;
  38: import java.util.zip.Deflater;
  39: 
  40: import org.jfree.repository.Repository;
  41: import org.jfree.repository.ContentLocation;
  42: import org.jfree.repository.ContentIOException;
  43: import org.jfree.repository.MimeRegistry;
  44: import org.jfree.repository.DefaultMimeRegistry;
  45: import org.jfree.io.IOUtils;
  46: 
  47: /**
  48:  * Creation-Date: 01.12.2006, 21:12:39
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class ZipRepository implements Repository
  53: {
  54:   private ZipOutputStream zipOutputStream;
  55:   private ZipEntryOutputStream currentStream;
  56:   private MimeRegistry mimeRegistry;
  57:   private ZipContentLocation root;
  58: 
  59:   public ZipRepository(final OutputStream out,
  60:                        final int level,
  61:                        final MimeRegistry mimeRegistry)
  62:   {
  63:     this.mimeRegistry = mimeRegistry;
  64:     this.zipOutputStream = new ZipOutputStream(out);
  65:     this.zipOutputStream.setLevel(level);
  66:     this.root = new ZipContentLocation(this, null, "");
  67:   }
  68: 
  69:   public ZipRepository(final OutputStream out,
  70:                        final int level)
  71:   {
  72:     this(out,level, new DefaultMimeRegistry());
  73:   }
  74: 
  75:   public ZipRepository(final OutputStream out)
  76:   {
  77:     this(out, Deflater.DEFAULT_COMPRESSION, new DefaultMimeRegistry());
  78:   }
  79: 
  80:   public ContentLocation getRoot() throws ContentIOException
  81:   {
  82:     return root;
  83:   }
  84: 
  85:   public MimeRegistry getMimeRegistry()
  86:   {
  87:     return mimeRegistry;
  88:   }
  89: 
  90:   public void close() throws IOException
  91:   {
  92:     zipOutputStream.finish();
  93:     zipOutputStream.flush();
  94:   }
  95: 
  96:   public void writeDirectory (final ZipEntry entry) throws IOException
  97:   {
  98:     zipOutputStream.putNextEntry(entry);
  99:   }
 100: 
 101:   public void writeContent (final ZipEntry entry, final InputStream in) throws IOException
 102:   {
 103:     zipOutputStream.putNextEntry(entry);
 104:     IOUtils.getInstance().copyStreams(in, zipOutputStream);
 105:     zipOutputStream.closeEntry();
 106:   }
 107: }