📄 fileoperutility.java
字号:
copyStream(inStream, outStream); } else if (sourceFile.isDirectory()) { if (destFile.exists() && !destFile.isDirectory()) { throw new IOException("cannot copy directory because there " + "is a file with the same name in destination: " + destFile.getPath()); } try { if (!destFile.exists() && !destFile.mkdirs()) { throw new IOException("cannot create local directory: " + destFile.getPath()); } File[] filelist = sourceFile.listFiles(); if (filelist == null) return; for (int i = 0; i < filelist.length; i++) { if (filelist[i].getPath().equals(destFile.getPath())) { return; } String destName = destFileName + File.separator + getRelativePath(filelist[i].getCanonicalPath(), sourceFile.getCanonicalPath()); copyLocalFile(filelist[i].getCanonicalPath(), destName); } } catch (Exception e) { throw new IOException("Cannot copy local directory: " + e.getMessage()); } } } /** * delete the directory * * @param dir directory to be deleted * @throws IOException */ public static void deleteDirTree(File dir) throws IOException { if (dir == null || !dir.isDirectory()) return; File[] filelist = dir.listFiles(); try { if (filelist == null) { if (!dir.delete()) { throw new IOException("Cannot delete directory: " + dir.getPath()); } return; } for (int i = 0; i < filelist.length; i++) { if (filelist[i].isFile()) { if (!filelist[i].delete()) { throw new IOException("Cannot delete file: " + filelist[i].getPath()); } } else if (filelist[i].isDirectory()) { deleteDirTree(filelist[i]); } } } catch (Exception e) { throw new IOException("Failed to delete directory: " + e.getMessage()); } } /** * Creates an unique local temp directory. * @return The name of the created directory. * @throws IOException If failed to create such a directory. */ public static String createUniqueTmpDir() throws IOException { // Create a unique temp directory. String tempDirPath = null; try { File sysTempDir = new File(System.getProperty("java.io.tmpdir")); File tempFile = File.createTempFile("jnlp", "jnlp", sysTempDir); tempDirPath = tempFile.getPath(); tempFile.delete(); tempFile = new File(tempDirPath); tempFile.mkdirs(); tempFile.deleteOnExit(); } catch (IOException e) { throw new IOException( "Failed to create a local temp directory: " + tempDirPath); } return tempDirPath + File.separator; } /** * get all the resource to local according to a remote jnlp file * * @param jnlp remote jnlp file location * @param localBase local directory which stores the resources * @return URL points to the local jnlp file * @throws IOException */ protected static URL getRemoteResource(URL jnlp, String localBase) throws IOException { URL localJnlpUrl = null; URL codebase = null; try { LaunchDesc laDesc = LaunchDescFactory.buildDescriptor(jnlp); codebase = laDesc.getCodebase(); String jnlpName = jnlp.getFile().substring( jnlp.getFile().lastIndexOf("/") + 1); File localJnlpFile = new File(localBase + File.separator + jnlpName); copyRemoteFile(jnlp, localJnlpFile); if (localJnlpFile == null || !localJnlpFile.exists()) { throw new IOException("Cannot copy remote jnlp file to " + "local"); } localJnlpUrl = localJnlpFile.toURL(); InformationDesc infoDesc = laDesc.getInformation(); IconDesc[] iconArray = infoDesc.getIcons(); for (int i = 0; i < iconArray.length; i++) { URL iconURL = iconArray[i].getLocation(); urlFile2LocalFile(iconURL, codebase, localBase); } ResourcesDesc reDesc = laDesc.getResources(); reDesc.visit( new JDICPackagerResourceCopyVisitor(codebase, localBase)); } catch (Exception e) { throw new IOException("Exception when geting remote resource: " + e.getMessage()); } return localJnlpUrl; } /** * Evaluate if the given directory is readable. * @param dirPath The given directory. * @return true If the given file is a valid directory and is readable. */ public static boolean isDirectoryReadable(File dirPath) { if (dirPath.isDirectory()) { if (dirPath.canRead()) { return true; } } return false; } /** * Evaluate if the given directory is writable. * @param dirPath The given direcotry to be evaluated. * @return true If the given file is a valid directory and is writableable. */ public static boolean isDirectoryWritable(File dirPath) { if (dirPath.isDirectory()) { if (dirPath.canWrite()) { return true; } } return false; } /** * Evaluate if the given directory is readable. * * @param filePath the given file to be evaluated * @return true is the filePath is readable, otherwise, false */ public static boolean isFileReadable(File filePath) { if (filePath.isFile()) { if (filePath.canRead()) { return true; } } return false; } /** * get the file name without extension * * @param filePath input file * @return filename without extension */ public static String getFileNameWithoutExt(File filePath) { if (filePath == null) return null; String fileName = filePath.getName(); if (fileName == null) return null; return (fileName.substring(0, fileName.lastIndexOf("."))); }}/** * Resource Visitor to copy all the remote resource files to the local base */ class JDICPackagerResourceCopyVisitor implements ResourceVisitor { private URL codebase = null; private String localBase = null; public JDICPackagerResourceCopyVisitor(URL incodebase, String inlocalbase) { codebase = incodebase; localBase = inlocalbase; } public void visitJARDesc(JARDesc jad) { try { FileOperUtility.urlFile2LocalFile(jad.getLocation(), codebase, localBase); } catch (IOException ioE) { ioE.printStackTrace(); } } public void visitExtensionDesc(ExtensionDesc ed) { try {/* FileOperUtility.urlFile2LocalFile(ed.getLocation(), codebase, localBase);*/ FileOperUtility.getRemoteResource(ed.getLocation(), localBase); } catch (IOException ioE) { ioE.printStackTrace(); } } public void visitPropertyDesc(PropertyDesc prd) {} public void visitJREDesc(JREDesc jrd) {} public void visitPackageDesc(PackageDesc pad) {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -