KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > FileUtil


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import org.radeox.util.logging.Logger;
28
29 import java.io.BufferedInputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.zip.Adler32 JavaDoc;
34 import java.util.zip.CheckedInputStream JavaDoc;
35
36 /**
37  *
38  * @author Matthias L. Jugel
39  * @version $Id: FileUtil.java 689 2003-02-03 14:07:06Z stephan $
40  */

41 public class FileUtil {
42
43   public static Checksum checksumDirectory(File JavaDoc file) throws IOException JavaDoc {
44     Checksum checksum = new Checksum(file.getAbsolutePath());
45     if (file.isDirectory()) {
46       checksumFiles(file, file.getAbsolutePath(), checksum);
47     } else {
48       checksumFile(file, file.getAbsolutePath(), checksum);
49     }
50     return checksum;
51   }
52
53
54   private static void checksumFiles(File JavaDoc file, String JavaDoc root, Checksum checksum) throws IOException JavaDoc {
55     File JavaDoc files[] = file.listFiles();
56     for (int i = 0; i < files.length; i++) {
57       if (files[i].isDirectory()) {
58         checksumFiles(files[i], root, checksum);
59       } else {
60         checksumFile(files[i], root, checksum);
61       }
62     }
63   }
64
65   private static void checksumFile(File JavaDoc file, String JavaDoc root, Checksum checksum) throws IOException JavaDoc {
66
67     CheckedInputStream JavaDoc fin = new CheckedInputStream JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file)),
68                                                     new Adler32 JavaDoc());
69     byte buffer[] = new byte[8192];
70     while ((fin.read(buffer)) != -1) {
71       /* ignore ... */
72     }
73     Long JavaDoc checkSum = new Long JavaDoc(fin.getChecksum().getValue());
74     checksum.add(file.getAbsolutePath().substring(root.length()+1), checkSum);
75     fin.close();
76   }
77
78   public static void main(String JavaDoc args[]) {
79     try {
80       Checksum checksum = checksumDirectory(new File JavaDoc(args[0]));
81       checksum.store(new File JavaDoc("./CHECKSUMS"));
82     } catch (IOException JavaDoc e) {
83       Logger.warn("FileUtil: usage: FileUtil jarfile", e);
84     }
85   }
86
87 }
88
Popular Tags