1 23 package org.archive.io.arc; 24 25 import it.unimi.dsi.fastutil.io.RepositionableStream; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.net.URI ; 33 import java.net.URISyntaxException ; 34 35 import org.archive.io.GzipHeader; 36 import org.archive.io.NoGzipMagicException; 37 import org.archive.net.UURI; 38 39 public class ARCUtils implements ARCConstants { 40 45 public static String parseArcFilename(final String pathOrUri) 46 throws URISyntaxException { 47 String path = pathOrUri; 48 if (UURI.hasScheme(pathOrUri)) { 49 URI url = new URI (pathOrUri); 50 path = url.getPath(); 51 } 52 return (new File (path)).getName(); 53 } 54 55 60 public static boolean isCompressed(File arcFile) throws IOException { 61 return testCompressedARCFile(arcFile); 62 } 63 64 76 public static boolean testCompressedARCFile(File arcFile) 77 throws IOException { 78 return testCompressedARCFile(arcFile, false); 79 } 80 81 94 public static boolean testCompressedARCFile(File arcFile, 95 boolean skipSuffixCheck) 96 throws IOException { 97 boolean compressedARCFile = false; 98 isReadable(arcFile); 99 if(!skipSuffixCheck && !arcFile.getName().toLowerCase() 100 .endsWith(COMPRESSED_ARC_FILE_EXTENSION)) { 101 return compressedARCFile; 102 } 103 104 final InputStream is = new FileInputStream (arcFile); 105 try { 106 compressedARCFile = testCompressedARCStream(is); 107 } finally { 108 is.close(); 109 } 110 return compressedARCFile; 111 } 112 113 120 public static boolean testCompressedARCStream(final InputStream is) 121 throws IOException { 122 boolean compressedARCFile = false; 123 GzipHeader gh = null; 124 try { 125 gh = new GzipHeader(is); 126 } catch (NoGzipMagicException e ) { 127 return compressedARCFile; 128 } 129 130 byte[] fextra = gh.getFextra(); 131 if (fextra != null && 136 ARC_GZIP_EXTRA_FIELD.length - 2 == fextra.length) { 137 compressedARCFile = true; 138 for (int i = 0; i < fextra.length; i++) { 139 if (fextra[i] != ARC_GZIP_EXTRA_FIELD[i + 2]) { 140 compressedARCFile = false; 141 break; 142 } 143 } 144 } 145 return compressedARCFile; 146 } 147 148 155 public static boolean testCompressedRepositionalStream( 156 final RepositionableStream rs) 157 throws IOException { 158 boolean compressedARCFile = false; 159 long p = rs.position(); 160 try { 161 compressedARCFile = testCompressedStream((InputStream )rs); 162 } finally { 163 rs.position(p); 164 } 165 return compressedARCFile; 166 } 167 168 175 public static boolean testCompressedStream(final InputStream is) 176 throws IOException { 177 boolean compressedARCFile = false; 178 try { 179 new GzipHeader(is); 180 compressedARCFile = true; 181 } catch (NoGzipMagicException e) { 182 return compressedARCFile; 183 } 184 return compressedARCFile; 185 } 186 187 198 public static boolean testUncompressedARCFile(File arcFile) 199 throws IOException { 200 boolean uncompressedARCFile = false; 201 isReadable(arcFile); 202 if(arcFile.getName().toLowerCase().endsWith(ARC_FILE_EXTENSION)) { 203 FileInputStream fis = new FileInputStream (arcFile); 204 try { 205 byte [] b = new byte[ARC_MAGIC_NUMBER.length()]; 206 int read = fis.read(b, 0, ARC_MAGIC_NUMBER.length()); 207 fis.close(); 208 if (read == ARC_MAGIC_NUMBER.length()) { 209 StringBuffer beginStr 210 = new StringBuffer (ARC_MAGIC_NUMBER.length()); 211 for (int i = 0; i < ARC_MAGIC_NUMBER.length(); i++) { 212 beginStr.append((char)b[i]); 213 } 214 215 if (beginStr.toString(). 216 equalsIgnoreCase(ARC_MAGIC_NUMBER)) { 217 uncompressedARCFile = true; 218 } 219 } 220 } finally { 221 fis.close(); 222 } 223 } 224 225 return uncompressedARCFile; 226 } 227 228 229 233 private static void isReadable(File arcFile) throws IOException { 234 if (!arcFile.exists()) { 235 throw new FileNotFoundException (arcFile.getAbsolutePath() + 236 " does not exist."); 237 } 238 239 if (!arcFile.canRead()) { 240 throw new FileNotFoundException (arcFile.getAbsolutePath() + 241 " is not readable."); 242 } 243 } 244 } 245 | Popular Tags |