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.Name;
38 import org.modeshape.graph.property.NameFactory;
39 import org.modeshape.graph.property.Path;
40 import org.modeshape.graph.property.PathFactory;
41 import org.modeshape.graph.sequencer.SequencerOutput;
42 import org.modeshape.graph.sequencer.StreamSequencer;
43 import org.modeshape.graph.sequencer.StreamSequencerContext;
44
45
46
47
48 public class ZipSequencer implements StreamSequencer {
49
50
51
52
53
54
55
56 public void sequence( InputStream stream,
57 SequencerOutput output,
58 StreamSequencerContext context ) {
59 BinaryFactory binaryFactory = context.getValueFactories().getBinaryFactory();
60 DateTimeFactory dateFactory = context.getValueFactories().getDateFactory();
61 PathFactory pathFactory = context.getValueFactories().getPathFactory();
62 NameFactory nameFactory = context.getValueFactories().getNameFactory();
63
64
65 Path pathToArchiveFile = context.getInputPath();
66 Name zipFileName = null;
67 if (pathToArchiveFile != null && !pathToArchiveFile.isRoot()) {
68
69 if (pathToArchiveFile.getLastSegment().getName().equals(JcrLexicon.CONTENT)) pathToArchiveFile = pathToArchiveFile.getParent();
70 if (!pathToArchiveFile.isRoot()) zipFileName = pathToArchiveFile.getLastSegment().getName();
71 }
72 if (zipFileName == null) {
73 zipFileName = ZipLexicon.CONTENT;
74 }
75
76 ZipInputStream in = null;
77 try {
78 in = new ZipInputStream(stream);
79 ZipEntry entry = in.getNextEntry();
80 byte[] buf = new byte[1024];
81
82
83 Path zipPath = pathFactory.createRelativePath(zipFileName);
84 output.setProperty(zipPath, JcrLexicon.PRIMARY_TYPE, ZipLexicon.CONTENT);
85 while (entry != null) {
86 Path entryPath = zipPath;
87 String entryName = entry.getName();
88 for (String segment : entryName.split(File.separator)) {
89 entryPath = pathFactory.create(entryPath, nameFactory.create(segment));
90 }
91
92 if (entry.isDirectory()) {
93 output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FOLDER);
94 } else {
95 output.setProperty(entryPath, JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.FILE);
96
97 Path contentPath = pathFactory.create(entryPath, JcrLexicon.CONTENT);
98 output.setProperty(contentPath, JcrLexicon.PRIMARY_TYPE, ModeShapeLexicon.RESOURCE);
99 int n;
100 ByteArrayOutputStream baout = new ByteArrayOutputStream();
101 while ((n = in.read(buf, 0, 1024)) > -1) {
102 baout.write(buf, 0, n);
103 }
104 byte[] bytes = baout.toByteArray();
105 output.setProperty(contentPath, JcrLexicon.DATA, binaryFactory.create(bytes));
106
107
108 output.setProperty(contentPath, JcrLexicon.ENCODED, "binary");
109 output.setProperty(contentPath, JcrLexicon.LAST_MODIFIED, dateFactory.create(entry.getTime()).toString());
110 output.setProperty(contentPath, JcrLexicon.MIMETYPE, "application/octet-stream");
111
112 }
113 in.closeEntry();
114 entry = in.getNextEntry();
115 }
116 } catch (Exception e) {
117 String location = context.getValueFactories().getStringFactory().create(context.getInputPath());
118 context.getProblems().addError(e, ZipI18n.errorReadingZipFile, location, e.getMessage());
119 } finally {
120 if (in != null) {
121 try {
122 in.close();
123 } catch (Exception e) {
124 String location = context.getValueFactories().getStringFactory().create(context.getInputPath());
125 context.getProblems().addError(e, ZipI18n.errorClosingZipFile, location, e.getMessage());
126 }
127 }
128 }
129 }
130 }