1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.modeshape.sequencer.zip;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.InputStream;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipInputStream;
32 import org.modeshape.graph.JcrLexicon;
33 import org.modeshape.graph.JcrNtLexicon;
34 import org.modeshape.graph.ModeShapeLexicon;
35 import org.modeshape.graph.property.BinaryFactory;
36 import org.modeshape.graph.property.DateTimeFactory;
37 import org.modeshape.graph.property.NameFactory;
38 import org.modeshape.graph.property.Path;
39 import org.modeshape.graph.property.PathFactory;
40 import org.modeshape.graph.sequencer.SequencerOutput;
41 import org.modeshape.graph.sequencer.StreamSequencer;
42 import org.modeshape.graph.sequencer.StreamSequencerContext;
43
44
45
46
47 public class ZipSequencer implements StreamSequencer {
48
49
50
51
52
53
54
55 public void sequence( InputStream stream,
56 SequencerOutput output,
57 StreamSequencerContext context ) {
58 BinaryFactory binaryFactory = context.getValueFactories().getBinaryFactory();
59 DateTimeFactory dateFactory = context.getValueFactories().getDateFactory();
60 PathFactory pathFactory = context.getValueFactories().getPathFactory();
61 NameFactory nameFactory = context.getValueFactories().getNameFactory();
62
63 try {
64 ZipInputStream in = new ZipInputStream(stream);
65 ZipEntry entry = in.getNextEntry();
66 byte[] buf = new byte[1024];
67
68
69 Path zipPath = pathFactory.createRelativePath(ZipLexicon.CONTENT);
70 output.setProperty(zipPath, JcrLexicon.PRIMARY_TYPE, ZipLexicon.CONTENT);
71 while (entry != null) {
72 Path entryPath = zipPath;
73 String entryName = entry.getName();
74 for (String segment : entryName.split(File.separator)) {
75 entryPath = pathFactory.create(entryPath, nameFactory.create(segment));
76 }
77
78 if (entry.isDirectory()) {
79 output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FOLDER);
80 } else {
81 output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FILE);
82
83 Path contentPath = pathFactory.create(entryPath, JcrLexicon.CONTENT);
84 output.setProperty(contentPath, JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.RESOURCE);
85 int n;
86 ByteArrayOutputStream baout = new ByteArrayOutputStream();
87 while ((n = in.read(buf, 0, 1024)) > -1) {
88 baout.write(buf, 0, n);
89 }
90 byte[] bytes = baout.toByteArray();
91 output.setProperty(contentPath, JcrLexicon.DATA, binaryFactory.create(bytes));
92
93
94 output.setProperty(contentPath, JcrLexicon.ENCODED, "binary");
95 output.setProperty(contentPath, JcrLexicon.LAST_MODIFIED,
96 dateFactory.create(entry.getTime()).toString());
97 output.setProperty(contentPath, JcrLexicon.MIMETYPE,
98 "application/octet-stream");
99
100 }
101 in.closeEntry();
102 entry = in.getNextEntry();
103
104 }
105 } catch (Exception e) {
106 e.printStackTrace();
107 }
108
109 }
110 }