001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix.tools.patcher;
021    
022    import java.io.*;
023    import java.util.zip.*;
024    
025    /**
026     * Computes released files' CRC32 and write them into the update.crc file.
027     *
028     * @author Emmanuel Bourg
029     * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
030     */
031    public class UpdateList
032    {
033        private static final String CRC_FILE = "update.crc";
034    
035        private static String path;
036        private static PrintWriter out;
037    
038        public static void main(String[] argv) throws IOException
039        {
040            if (argv.length > 0)
041            {
042                path = argv[0];
043            }
044            else
045            {
046                path = ".";
047            }
048    
049            out = new PrintWriter(new FileWriter(path + File.separator + CRC_FILE), true);
050    
051            browseDirectory(new File(path));
052    
053            out.close();
054        }
055    
056    
057        /**
058         * Recurse through directories and output files CRCs
059         *
060         * @param directory base directory
061         */
062        public static void browseDirectory(File directory) throws IOException
063        {
064            File listeFichiers[] = directory.listFiles();
065    
066            for (int i = 0; i < listeFichiers.length; i++)
067            {
068                File f = listeFichiers[i];
069    
070                if (f.isFile())
071                {
072                    String name = f.toString().substring(path.toString().length() + 1);
073    
074                    if (!CRC_FILE.equals(name))
075                    {
076                        out.println(name + "\t" + getFileCRC32(f) + "\t" + f.length());
077                    }
078                }
079                else
080                {
081                    browseDirectory(f);
082                }
083            }
084        }
085    
086    
087        /**
088         * Compute CRC32 for the specified file.
089         *
090         * @param file
091         *
092         * @return CRC32
093         */
094        public static long getFileCRC32(File file) throws IOException
095        {
096            if (file.exists() && file.isFile())
097            {
098                FileInputStream fis = new FileInputStream(file);
099    
100                CRC32 check = new CRC32();
101    
102                int b = fis.read();
103    
104                while (b != -1)
105                {
106                    b = fis.read();
107                    check.update(b);
108                }
109    
110                fis.close();
111    
112                return check.getValue();
113            }
114            else
115            {
116                return 0;
117            }
118        }
119    
120    }