1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.FilterOutputStream ; 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 17 23 public class TarOutputStream extends FilterOutputStream { 24 25 private int byteswritten = 0; 26 private int datapos = 0; 27 private long cursize = 0; 28 29 34 public TarOutputStream(OutputStream out) { 35 super(out); 36 } 37 38 41 public void close() throws IOException { 42 byte[] zeros = new byte[1024]; 44 cursize = 1024; 45 write(zeros, 0, 1024); 46 47 if((byteswritten % 10240) != 0) { 50 int length = 10240 - (byteswritten % 10240); 51 cursize = length; 52 zeros = new byte[length]; 53 write(zeros, 0, length); 54 } 55 super.close(); 56 } 57 58 64 public void closeEntry() throws IOException { 65 byte[] data = new byte[512]; 66 int len = 512 - datapos; 67 if(len > 0 && datapos > 0) { 68 cursize = len; 69 write(data, 0, len); 70 } 71 } 72 73 80 private long headerChecksum(byte[] header) { 81 long sum = 0; 82 for(int i = 0; i < 512; i++) { 83 sum += header[i] & 0xff; 84 } 85 return sum; 86 } 87 88 94 public void putNextEntry(TarEntry e) throws IOException { 95 byte[] header = new byte[512]; 96 String filename = e.getName(); 97 String prefix = null; 98 int pos, i; 99 100 101 byte[] filenameBytes = filename.getBytes("UTF8"); if (filenameBytes.length > 99) { 103 int seppos = filename.lastIndexOf('/'); 104 if(seppos == -1) { 105 throw new IOException ("filename too long"); } 107 prefix = filename.substring(0, seppos); 108 filename = filename.substring(seppos + 1); 109 filenameBytes = filename.getBytes("UTF8"); if (filenameBytes.length > 99) { 111 throw new IOException ("filename too long"); } 113 } 114 115 116 pos = 0; 117 System.arraycopy(filenameBytes, 0, header, 0, filenameBytes.length); 118 pos += 100; 119 120 121 StringBuffer mode = new StringBuffer (Long.toOctalString(e.getMode())); 122 while(mode.length() < 7) { 123 mode.insert(0,'0'); 124 } 125 for(i = 0; i < 7; i++) { 126 header[pos + i] = (byte) mode.charAt(i); 127 } 128 pos += 8; 129 130 131 header[pos] = '0'; 132 pos += 8; 133 134 135 header[pos] = '0'; 136 pos += 8; 137 138 139 String length = Long.toOctalString(e.getSize()); 140 for(i = 0; i < length.length(); i++) { 141 header[pos + i] = (byte) length.charAt(i); 142 } 143 pos += 12; 144 145 146 String mtime = Long.toOctalString(e.getTime()); 147 for(i = 0; i < mtime.length(); i++) { 148 header[pos + i] = (byte) mtime.charAt(i); 149 } 150 pos += 12; 151 152 153 for(i = 0; i < 8; i++) { 154 header[pos + i] = ' '; 155 } 156 pos += 8; 157 158 159 header[pos] = (byte) e.getFileType(); 160 pos += 1; 161 162 163 pos += 100; 164 165 166 String ustar = "ustar 00"; for(i = 0; i < ustar.length(); i++) { 168 header[pos + i] = (byte) ustar.charAt(i); 169 } 170 header[pos + 5] = 0; 171 pos += 8; 172 173 174 String uname = "nobody"; for(i = 0; i < uname.length(); i++) { 176 header[pos + i] = (byte) uname.charAt(i); 177 } 178 pos += 32; 179 180 181 182 String gname = "nobody"; for(i = 0; i < gname.length(); i++) { 184 header[pos + i] = (byte) gname.charAt(i); 185 } 186 pos += 32; 187 188 189 pos += 8; 190 191 192 pos += 8; 193 194 195 if(prefix != null) { 196 byte[] prefixBytes = prefix.getBytes("UTF8"); if (prefixBytes.length > 155) { 198 throw new IOException ("prefix too large"); } 200 System.arraycopy(prefixBytes, 0, header, pos, prefixBytes.length); 201 } 202 203 long sum = headerChecksum(header); 204 pos = 100 + 8 + 8 + 8 + 12 + 12; 205 String sumval = Long.toOctalString(sum); 206 for(i = 0; i < sumval.length(); i++) { 207 header[pos + i] = (byte) sumval.charAt(i); 208 } 209 210 cursize = 512; 211 write(header, 0, 512); 212 213 cursize = e.getSize(); 214 } 215 216 219 public void write(byte[] b, int off, int len) throws IOException { 220 super.write(b, off, len); 221 datapos = (datapos + len) % 512; 222 byteswritten += len; 223 cursize -= len; 224 if(cursize < 0) { 225 throw new IOException ("too much data written for current file"); } 227 } 228 } 229 | Popular Tags |