001 /*
002 * JBoss DNA (http://www.jboss.org/dna)
003 * See the COPYRIGHT.txt file distributed with this work for information
004 * regarding copyright ownership. Some portions may be licensed
005 * to Red Hat, Inc. under one or more contributor license agreements.
006 * See the AUTHORS.txt file in the distribution for a full listing of
007 * individual contributors.
008 *
009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010 * is licensed to you under the terms of the GNU Lesser General Public License as
011 * published by the Free Software Foundation; either version 2.1 of
012 * the License, or (at your option) any later version.
013 *
014 * JBoss DNA is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017 * Lesser General Public License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this software; if not, write to the Free
021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023 */
024
025 package org.jboss.dna.sequencer.zip;
026
027 import java.io.ByteArrayOutputStream;
028 import java.io.InputStream;
029 import java.util.zip.ZipEntry;
030 import java.util.zip.ZipInputStream;
031 import org.jboss.dna.graph.sequencer.SequencerContext;
032 import org.jboss.dna.graph.sequencer.SequencerOutput;
033 import org.jboss.dna.graph.sequencer.StreamSequencer;
034
035 /**
036 * @author Michael Trezzi
037 * @author John Verhaeg
038 */
039 public class ZipSequencer implements StreamSequencer {
040
041 /**
042 * {@inheritDoc}
043 *
044 * @see org.jboss.dna.graph.sequencer.StreamSequencer#sequence(java.io.InputStream,
045 * org.jboss.dna.graph.sequencer.SequencerOutput, org.jboss.dna.graph.sequencer.SequencerContext)
046 */
047 public void sequence( InputStream stream,
048 SequencerOutput output,
049 SequencerContext context ) {
050 try {
051 ZipInputStream in = new ZipInputStream(stream);
052 ZipEntry entry = in.getNextEntry();
053 byte[] buf = new byte[1024];
054
055 // Create top-level node
056 output.setProperty("zip:content", "jcr:primaryType", "zip:content");
057 while (entry != null) {
058
059 if (entry.isDirectory()) { // If entry is directory, create nt:folder node
060 output.setProperty("zip:content/" + entry.getName(), "jcr:primaryType", "nt:folder");
061 } else { // If entry is File, create nt:file
062 output.setProperty("zip:content/" + entry.getName() + "/jcr:content", "jcr:primaryType", "nt:resource");
063 int n;
064 ByteArrayOutputStream baout = new ByteArrayOutputStream();
065 while ((n = in.read(buf, 0, 1024)) > -1) {
066 baout.write(buf, 0, n);
067 }
068 output.setProperty("zip:content/" + entry.getName() + "/jcr:content", "jcr:data", baout.toByteArray());
069 // all other nt:file properties should be generated by other sequencers (mimetype, encoding,...)
070 }
071 in.closeEntry();
072 entry = in.getNextEntry();
073
074 }
075 } catch (Exception e) {
076 e.printStackTrace();
077 }
078
079 }
080 }