001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.util.io; 025 026 import java.io.BufferedInputStream; 027 import java.io.File; 028 import java.io.FileInputStream; 029 import java.util.ArrayList; 030 import java.util.HashMap; 031 import java.util.LinkedList; 032 import java.util.List; 033 import java.util.Map; 034 035 import org.ujmp.core.util.MathUtil; 036 037 public class FileUtil { 038 039 public static boolean deleteRecursive(File path) { 040 if (path != null && path.exists()) { 041 File[] files = path.listFiles(); 042 for (File f : files) { 043 if (f.isDirectory()) { 044 deleteRecursive(f); 045 } else { 046 f.delete(); 047 } 048 } 049 } 050 if (path != null) { 051 return path.delete(); 052 } else { 053 return false; 054 } 055 } 056 057 // TODO: this can be made faster by reading into a byte buffer 058 public static boolean equalsContent(File file1, File file2) throws Exception { 059 if (file1.length() != file2.length()) { 060 return false; 061 } 062 boolean areEqual = true; 063 BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(file1)); 064 BufferedInputStream in2 = new BufferedInputStream(new FileInputStream(file2)); 065 while (true) { 066 int i1 = in1.read(); 067 int i2 = in2.read(); 068 if (i1 != i2) { 069 areEqual = false; 070 break; 071 } 072 if (i1 == -1) { 073 break; 074 } 075 } 076 in1.close(); 077 in2.close(); 078 return areEqual; 079 } 080 081 public static void move(File source, File target) { 082 083 } 084 085 public static String md5Sum(File file) throws Exception { 086 return MathUtil.md5(file); 087 } 088 089 public static List<List<File>> findDuplicates(File path) throws Exception { 090 return findDuplicates(path, new HashMap<String, List<File>>()); 091 } 092 093 private static List<List<File>> findDuplicates(File path, Map<String, List<File>> md5Map) 094 throws Exception { 095 List<List<File>> list = new ArrayList<List<File>>(); 096 File[] files = path.listFiles(); 097 for (File file : files) { 098 if (file.isDirectory()) { 099 List<List<File>> subDirList = findDuplicates(file); 100 list.addAll(subDirList); 101 } else { 102 System.out.print(file); 103 String md5 = md5Sum(file); 104 System.out.println(" [" + md5 + "]"); 105 List<File> similarFiles = md5Map.get(md5); 106 if (similarFiles == null) { 107 similarFiles = new LinkedList<File>(); 108 md5Map.put(md5, similarFiles); 109 } 110 if (!similarFiles.isEmpty()) { 111 System.out.println(" " + similarFiles.size() + " files with same md5"); 112 for (File similarFile : similarFiles) { 113 if (equalsContent(file, similarFile)) { 114 System.out.println(" match found: " + similarFile); 115 List<File> set = new LinkedList<File>(); 116 set.add(file); 117 set.add(similarFile); 118 list.add(set); 119 } 120 } 121 } 122 similarFiles.add(file); 123 } 124 } 125 return list; 126 } 127 128 public static int countFiles(File path) { 129 int count = 0; 130 File[] files = path.listFiles(); 131 if (files != null) { 132 for (File f : files) { 133 if (f.isDirectory()) { 134 count += countFiles(f); 135 } else { 136 count++; 137 } 138 } 139 } 140 return count; 141 } 142 143 }