# HG changeset patch # User Ingo Weinzierl # Date 1314282659 0 # Node ID 666417d5781c94f990e3e914c6bcd472cd68a00f # Parent 1eb7863136f435a9d15e5aaf9d6aab031d6c4f8e Inserted a new utility class FileTools to work with files. artifacts/trunk@2584 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 1eb7863136f4 -r 666417d5781c ChangeLog --- a/ChangeLog Thu Aug 25 14:22:41 2011 +0000 +++ b/ChangeLog Thu Aug 25 14:30:59 2011 +0000 @@ -1,3 +1,8 @@ +2011-08-25 Ingo Weinzierl + + * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java: + New. A utility class for working with Files. + 2011-08-25 Sascha L. Teichmann * artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java diff -r 1eb7863136f4 -r 666417d5781c artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Thu Aug 25 14:30:59 2011 +0000 @@ -0,0 +1,403 @@ +/* + * Copyright (c) 2010, 2011 by Intevation GmbH + * + * This program is free software under the LGPL (>=v2.1) + * Read the file LGPL.txt coming with the software for details + * or visit http://www.gnu.org/licenses/ if it does not exist. + */ +package de.intevation.artifacts.common.utils; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.FileInputStream; +import java.io.OutputStream; + +import java.util.Stack; +import java.util.List; +import java.util.Set; +import java.util.HashSet; +import java.util.ArrayList; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.apache.log4j.Logger; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class FileTools +{ + private static Logger log = Logger.getLogger(FileTools.class); + + public static final String DIGEST = + System.getProperty("artifacts.common.file.cmp.digest", "MD5"); + + private FileTools() { + } + + + public static File getDirectory(String path, String name) { + if (path == null || name == null) { + return null; + } + + File dir = new File(path, name); + + if (!dir.exists()) { + log.debug( + "Directory '" + dir.getAbsolutePath() + "' doesn't " + + "exist. Try to create it."); + + return dir.mkdir() ? dir : null; + } + else { + return dir.isDirectory() ? dir : null; + } + } + + public static File repair(File file) { + file = file.getAbsoluteFile(); + if (file.exists()) { + return file; + } + Stack parts = new Stack(); + File curr = file; + while (curr != null) { + String name = curr.getName(); + if (name.length() > 0) { + parts.push(curr.getName()); + } + curr = curr.getParentFile(); + } + + curr = null; + OUTER: while (!parts.isEmpty()) { + String f = parts.pop(); + log.debug("fixing: '" + f + "'"); + if (curr == null) { + // XXX: Not totaly correct because there + // more than one root on none unix systems. + for (File root: File.listRoots()) { + File [] files = root.listFiles(); + if (files == null) { + log.warn("cannot list '" + root); + continue; + } + for (File candidate: files) { + if (candidate.getName().equalsIgnoreCase(f)) { + curr = new File(root, candidate.getName()); + continue OUTER; + } + } + } + break; + } + else { + File [] files = curr.listFiles(); + if (files == null) { + log.warn("cannot list: '" + curr + "'"); + return file; + } + for (File candidate: files) { + if (candidate.getName().equalsIgnoreCase(f)) { + curr = new File(curr, candidate.getName()); + continue OUTER; + } + } + curr = null; + break; + } + } + + if (curr == null) { + log.warn("cannot repair path '" + file + "'"); + return file; + } + + return curr; + } + + public static class HashedFile + implements Comparable + { + protected File file; + protected long length; + protected byte [] hash; + + public HashedFile(File file) { + this.file = file; + length = file.length(); + } + + public File getFile() { + return file; + } + + protected byte [] getHash() { + if (hash == null) { + InputStream in = null; + + try { + in = new FileInputStream(file); + + MessageDigest digest = MessageDigest.getInstance(DIGEST); + + byte [] buf = new byte[40*1024]; + int r; + + while ((r = in.read(buf)) >= 0) { + digest.update(buf, 0, r); + } + + hash = digest.digest(); + } + catch (IOException ioe) { + log.error(ioe); + hash = new byte[0]; + } + catch (NoSuchAlgorithmException nsae) { + log.error(nsae); + hash = new byte[0]; + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ioe) { + log.error(ioe); + } + } + } + } + return hash; + } + + @Override + public int compareTo(HashedFile other) { + if (length < other.length) return -1; + if (length > other.length) return +1; + return compare(getHash(), other.getHash()); + } + + private static int compare(byte [] a, byte [] b) { + if (a.length < b.length) return -1; + if (a.length > b.length) return +1; + for (int i = 0; i < a.length; ++i) { + int x = a[i] & 0xff; + int y = b[i] & 0xff; + if (x < y) return -1; + if (x > y) return +1; + } + return 0; + } + + @Override + public boolean equals(Object other) { + return other instanceof HashedFile + && ((HashedFile)other).compareTo(this) == 0; + } + + @Override + public int hashCode() { + return (int)(length ^ (length >>> 32)); + } + } // class HashedFile + + public static List uniqueFiles(List files) { + + Set set = new HashSet(); + + for (File file: files) { + if (!set.add(new HashedFile(file))) { + log.warn("file '" + file + "' is a duplicate."); + } + } + + ArrayList out = new ArrayList(set.size()); + for (HashedFile hf: set) { + out.add(hf.file); + } + + return out; + } + + public interface FileVisitor { + boolean visit(File file); + } // Visitor + + public static void walkTree(File root, FileVisitor visitor) { + + Stack stack = new Stack(); + + stack.push(root); + + while (!stack.isEmpty()) { + File current = stack.pop(); + if (!visitor.visit(current)) break; + if (current.isDirectory()) { + File [] subs = current.listFiles(); + if (subs != null) { + for (File f: subs) { + stack.push(f); + } + } + } + } + } + + /** + * Deletes everything in a directory. + * + * @param dir The directory. + */ + public final static void deleteContent(File dir) { + if (dir == null || !dir.isDirectory()) { + return; + } + + File[] files = dir.listFiles(); + if (files != null) { + for (File file: files) { + deleteRecursive(file); + } + } + + return; + } + + /** + * Delete file and everything in file if it is a directory. + * + * @param file The file or directory. + * @return true, if deletion was successful - otherwise false. + */ + public final static boolean deleteRecursive(File file) { + + if (file == null) { + return false; + } + + if (file.isDirectory()) { + File [] files = file.listFiles(); + if (files != null) { + for (File sub: files) { + if (!deleteRecursive(sub)) { + return false; + } + } + } + } + + return file.delete(); + } + + /** + * Put the given file or directory into a zip archive. + * + * @param file The file or directory. + * @param outputStream The stream to write the archive to. + * @throws IOException if an error occured while zip creation or writing to + * output stream. + */ + public static void createZipArchive( + File file, + OutputStream outputStream + ) + throws IOException + { + ZipOutputStream out = new ZipOutputStream(outputStream); + + if (file.isFile()) { + copyFileToZip("", file, out); + } + else if (file.isDirectory()) { + + Stack stack = new Stack(); + stack.push(new PrefixDir(file.getName() + "/", file)); + + while (!stack.isEmpty()) { + PrefixDir pd = stack.pop(); + + ZipEntry dirEntry = new ZipEntry(pd.prefix); + out.putNextEntry(dirEntry); + out.closeEntry(); + + File [] files = pd.dir.listFiles(); + if (files != null) { + for (File sub: files) { + if (sub.isDirectory()) { + stack.push(new PrefixDir( + pd.prefix + sub.getName() + "/", + sub)); + } + else if (sub.isFile()) { + copyFileToZip(pd.prefix, sub, out); + } + } + } + } + } + + out.finish(); + } + + /** + * A class representing a directory with a prefix. + */ + private static final class PrefixDir { + + String prefix; + File dir; + + public PrefixDir(String prefix, File dir) { + this.prefix = prefix; + this.dir = dir; + } + + } // class PrefixDir + + /** + * Write a file to zip archive. + * + * @param prefix A prefix. + * @param file The file. + * @param out The output stream. + * @throws IOException if an error occured while writing to zip output + * stream. + */ + private static void copyFileToZip( + String prefix, + File file, + ZipOutputStream out + ) + throws IOException + { + String entryName = prefix + file.getName(); + ZipEntry entry = new ZipEntry(entryName); + out.putNextEntry(entry); + InputStream in = null; + try { + in = + new BufferedInputStream( + new FileInputStream(file), 20*1024); + + byte [] buf = new byte[2048]; + + int r; + while ((r = in.read(buf)) > 0) { + out.write(buf, 0, r); + } + } + finally { + if (in != null) { + try { in.close(); } + catch (IOException ioe) {} + } + } + out.closeEntry(); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :