1 2 17 18 19 package org.apache.poi.poifs.filesystem; 20 21 import java.util.*; 22 23 import java.io.*; 24 25 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 26 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 27 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 28 29 35 36 public class ReaderWriter 37 implements POIFSReaderListener, POIFSWriterListener 38 { 39 private POIFSFileSystem filesystem; 40 private DirectoryEntry root; 41 42 private Map dataMap; 44 45 52 53 ReaderWriter(final POIFSFileSystem filesystem) 54 { 55 this.filesystem = filesystem; 56 root = this.filesystem.getRoot(); 57 dataMap = new HashMap(); 58 } 59 60 69 70 public static void main(String [] args) 71 throws IOException 72 { 73 if (args.length != 2) 74 { 75 System.err.println( 76 "two arguments required: one input file name and one output file name"); 77 } 78 else 79 { 80 POIFSReader reader = new POIFSReader(); 81 POIFSFileSystem filesystem = new POIFSFileSystem(); 82 83 reader.registerListener(new ReaderWriter(filesystem)); 84 FileInputStream istream = new FileInputStream(args[ 0 ]); 85 86 reader.read(istream); 87 istream.close(); 88 FileOutputStream ostream = new FileOutputStream(args[ 1 ]); 89 90 filesystem.writeFilesystem(ostream); 91 ostream.close(); 92 } 93 } 94 95 96 97 103 104 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 105 { 106 DocumentInputStream istream = event.getStream(); 107 POIFSDocumentPath path = event.getPath(); 108 String name = event.getName(); 109 110 try 111 { 112 int size = istream.available(); 113 byte[] data = new byte[ istream.available() ]; 114 115 istream.read(data); 116 DocumentDescriptor descriptor = new DocumentDescriptor(path, 117 name); 118 119 System.out.println("adding document: " + descriptor + " (" + size 120 + " bytes)"); 121 dataMap.put(descriptor, data); 122 int pathLength = path.length(); 123 DirectoryEntry entry = root; 124 125 for (int k = 0; k < path.length(); k++) 126 { 127 String componentName = path.getComponent(k); 128 Entry nextEntry = null; 129 130 try 131 { 132 nextEntry = entry.getEntry(componentName); 133 } 134 catch (FileNotFoundException ignored) 135 { 136 try 137 { 138 nextEntry = entry.createDirectory(componentName); 139 } 140 catch (IOException e) 141 { 142 System.out.println("Unable to create directory"); 143 e.printStackTrace(); 144 System.exit(1); 145 } 146 } 147 entry = ( DirectoryEntry ) nextEntry; 148 } 149 entry.createDocument(name, size, this); 150 } 151 catch (IOException ignored) 152 { 153 } 154 } 155 156 157 158 159 165 166 public void processPOIFSWriterEvent(final POIFSWriterEvent event) 167 { 168 try 169 { 170 DocumentDescriptor descriptor = 171 new DocumentDescriptor(event.getPath(), event.getName()); 172 173 System.out.println("looking up document: " + descriptor + " (" 174 + event.getLimit() + " bytes)"); 175 event.getStream().write(( byte [] ) dataMap.get(descriptor)); 176 } 177 catch (IOException e) 178 { 179 System.out.println("Unable to write document"); 180 e.printStackTrace(); 181 System.exit(1); 182 } 183 } 184 185 186 } 188 | Popular Tags |