001    /**
002     * ===========================================================
003     * LibRepository : a free Java content repository access layer
004     * ===========================================================
005     *
006     * Project Info:  http://jfreereport.pentaho.org/librepository/
007     *
008     * (C) Copyright 2006, by Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * ZipRepository.java
027     * ------------
028     * (C) Copyright 2006, by Pentaho Corporation.
029     */
030    
031    package org.jfree.repository.zipwriter;
032    
033    import java.io.OutputStream;
034    import java.io.IOException;
035    import java.io.InputStream;
036    import java.util.zip.ZipOutputStream;
037    import java.util.zip.ZipEntry;
038    import java.util.zip.Deflater;
039    
040    import org.jfree.repository.Repository;
041    import org.jfree.repository.ContentLocation;
042    import org.jfree.repository.ContentIOException;
043    import org.jfree.repository.MimeRegistry;
044    import org.jfree.repository.DefaultMimeRegistry;
045    import org.jfree.io.IOUtils;
046    
047    /**
048     * Creation-Date: 01.12.2006, 21:12:39
049     *
050     * @author Thomas Morgner
051     */
052    public class ZipRepository implements Repository
053    {
054      private ZipOutputStream zipOutputStream;
055      private ZipEntryOutputStream currentStream;
056      private MimeRegistry mimeRegistry;
057      private ZipContentLocation root;
058    
059      public ZipRepository(final OutputStream out,
060                           final int level,
061                           final MimeRegistry mimeRegistry)
062      {
063        this.mimeRegistry = mimeRegistry;
064        this.zipOutputStream = new ZipOutputStream(out);
065        this.zipOutputStream.setLevel(level);
066        this.root = new ZipContentLocation(this, null, "");
067      }
068    
069      public ZipRepository(final OutputStream out,
070                           final int level)
071      {
072        this(out,level, new DefaultMimeRegistry());
073      }
074    
075      public ZipRepository(final OutputStream out)
076      {
077        this(out, Deflater.DEFAULT_COMPRESSION, new DefaultMimeRegistry());
078      }
079    
080      public ContentLocation getRoot() throws ContentIOException
081      {
082        return root;
083      }
084    
085      public MimeRegistry getMimeRegistry()
086      {
087        return mimeRegistry;
088      }
089    
090      public void close() throws IOException
091      {
092        zipOutputStream.finish();
093        zipOutputStream.flush();
094      }
095    
096      public void writeDirectory (final ZipEntry entry) throws IOException
097      {
098        zipOutputStream.putNextEntry(entry);
099      }
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    }