Subversion Repositories bacoAlunos

Rev

Rev 1830 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 jmachado 1
package jomm.utils;
2
 
1845 jmachado 3
import org.apache.log4j.Logger;
4
 
214 jmachado 5
import java.io.*;
1845 jmachado 6
import java.net.URISyntaxException;
7
import java.net.URL;
8
import java.net.URLDecoder;
1814 jmachado 9
import java.security.DigestInputStream;
1675 jmachado 10
import java.security.MessageDigest;
214 jmachado 11
import java.util.zip.ZipEntry;
1830 jmachado 12
import java.util.zip.ZipInputStream;
214 jmachado 13
import java.util.zip.ZipOutputStream;
14
 
207 jmachado 15
/**
16
 * @author Jorge Machado
17
 * @date 20/Jun/2008
18
 * @see jomm.utils
19
 */
20
public class FilesUtils
21
{
1845 jmachado 22
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(FilesUtils.class);
23
 
207 jmachado 24
    public static 1.5.0/docs/api/java/lang/String.html">String getExtension(1.5.0/docs/api/java/lang/String.html">String filename)
25
    {
1845 jmachado 26
 
207 jmachado 27
        1.5.0/docs/api/java/lang/String.html">String extension = "";
28
        if(filename == null)
29
            return extension;
30
        if(filename.indexOf(".")>=0)
31
            extension = filename.substring(filename.lastIndexOf(".") + 1);
32
        return extension;
33
    }
214 jmachado 34
 
35
    public static void copy(5+0%2Fdocs%2Fapi+InputStream">InputStream src, 1.5.0/docs/api/java/lang/String.html">String outputPath) throws 1.5.0/docs/api/java/io/IOException.html">IOException
36
    {
37
        1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(new 1.5.0/docs/api/java/io/File.html">File(outputPath));
38
        StreamsUtils.inputStream2OutputStream(src,out);
39
    }
40
 
223 jmachado 41
 
214 jmachado 42
    public static void zipFolder(1.5.0/docs/api/java/lang/String.html">String folder, 1.5.0/docs/api/java/lang/String.html">String outFilename) throws 1.5.0/docs/api/java/io/IOException.html">IOException
43
    {
44
        1.5.0/docs/api/java/util/zip/ZipOutputStream.html">ZipOutputStream out = new 1.5.0/docs/api/java/util/zip/ZipOutputStream.html">ZipOutputStream(new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(outFilename));
45
        zipFolder(folder,"",out);
46
        out.close();
47
    }
48
 
1830 jmachado 49
 
50
 
214 jmachado 51
    private static void zipFolder(1.5.0/docs/api/java/lang/String.html">String folder, 1.5.0/docs/api/java/lang/String.html">String relativePath, 1.5.0/docs/api/java/util/zip/ZipOutputStream.html">ZipOutputStream out) throws 1.5.0/docs/api/java/io/IOException.html">IOException
52
    {
53
        1.5.0/docs/api/java/io/File.html">File zipFolder = new 1.5.0/docs/api/java/io/File.html">File(folder);
54
        // Compress the files
55
        1.5.0/docs/api/java/io/File.html">File[] files = zipFolder.listFiles();
56
        for(1.5.0/docs/api/java/io/File.html">File file : files)
57
        {
58
            if (file.isDirectory())
59
            {
60
                zipFolder(folder + "/" + file.getName(), relativePath + "/" + file.getName(),out);
61
            }
62
            else
63
            {
64
                1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream in = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file);
65
                // Add ZIP entry to output stream.
66
                out.putNextEntry(new 1.5.0/docs/api/java/util/zip/ZipEntry.html">ZipEntry(relativePath + "/" +  file.getName()));
67
                // Transfer bytes from the file to the ZIP file
68
                int len;
69
                byte[] buf = new byte[1024];
70
                while ((len = in.read(buf)) > 0)
71
                {
72
                    out.write(buf, 0, len);
73
                }
74
                // Complete the entry
75
                out.closeEntry();
76
                in.close();
77
            }
78
        }
79
    }
80
 
1830 jmachado 81
    public static void uzip2Folder(1.5.0/docs/api/java/lang/String.html">String folder, 1.5.0/docs/api/java/util/zip/ZipInputStream.html">ZipInputStream input) throws 1.5.0/docs/api/java/io/IOException.html">IOException
82
    {
83
        1.5.0/docs/api/java/io/File.html">File zipFolder = new 1.5.0/docs/api/java/io/File.html">File(folder);
84
        if(!zipFolder.exists()) zipFolder.mkdirs();
85
        1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream fis;
86
        //buffer for read and write data to file
87
        byte[] buffer = new byte[1024];
88
        try {
89
            1.5.0/docs/api/java/util/zip/ZipEntry.html">ZipEntry ze = input.getNextEntry();
90
            while(ze != null){
91
                1.5.0/docs/api/java/lang/String.html">String fileName = ze.getName();
92
                1.5.0/docs/api/java/io/File.html">File newFile = new 1.5.0/docs/api/java/io/File.html">File(folder + 1.5.0/docs/api/java/io/File.html">File.separator + fileName);
93
                1.5.0/docs/api/java/lang/System.html">System.out.println("Unzipping to "+newFile.getAbsolutePath());
94
                //create directories for sub directories in zip
95
                new 1.5.0/docs/api/java/io/File.html">File(newFile.getParent()).mkdirs();
96
                1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream fos = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(newFile);
97
                int len;
98
                while ((len = input.read(buffer)) > 0) {
99
                    fos.write(buffer, 0, len);
100
                }
101
                fos.close();
102
                //close this ZipEntry
103
                input.closeEntry();
104
                ze = input.getNextEntry();
105
            }
106
            //close last ZipEntry
107
            input.closeEntry();
108
            input.close();
109
        } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
110
            e.printStackTrace();
111
        }
112
    }
113
 
223 jmachado 114
    public static synchronized void deleteOldContent(1.5.0/docs/api/java/lang/String.html">String folder)
115
    {
116
        deleteOldContent(new 1.5.0/docs/api/java/io/File.html">File(folder));
117
    }
118
 
119
    public static synchronized void deleteOldContent(1.5.0/docs/api/java/io/File.html">File folder)
120
    {
121
        if(folder.isDirectory())
122
        {
123
            for(1.5.0/docs/api/java/io/File.html">File f : folder.listFiles())
124
            {
125
                deleteOld(f);
126
            }
127
        }
128
    }
214 jmachado 129
    public static void delete(1.5.0/docs/api/java/lang/String.html">String deleteFile)
130
    {
131
        delete(new 1.5.0/docs/api/java/io/File.html">File(deleteFile));
132
    }
133
    public static void delete(1.5.0/docs/api/java/io/File.html">File toDelete)
134
    {
135
        if(toDelete.isFile())
136
            toDelete.delete();
137
        else
138
        {
139
            for(1.5.0/docs/api/java/io/File.html">File f : toDelete.listFiles())
140
            {
141
                delete(f);
142
            }
143
            toDelete.delete();
144
        }
145
    }
223 jmachado 146
 
147
    public static final long hourMili = 1000 * 60 * 60 * 24;
148
 
149
    public static boolean deleteOld(1.5.0/docs/api/java/io/File.html">File toDelete)
150
    {
151
        boolean allEliminated = true;
152
        if(toDelete.isFile() && 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - toDelete.lastModified() > hourMili)
153
            toDelete.delete();
154
        else if(toDelete.isFile())
155
            allEliminated = false;
156
        else
157
        {
158
            for(1.5.0/docs/api/java/io/File.html">File f : toDelete.listFiles())
159
            {
160
                if(!deleteOld(f))
161
                    allEliminated = false;
162
            }
163
            if(allEliminated)
164
                toDelete.delete();
165
        }
166
        return allEliminated;
167
    }
1675 jmachado 168
 
169
 
170
    public static byte[] createChecksum(1.5.0/docs/api/java/io/File.html">File file) throws 1.5.0/docs/api/java/lang/Exception.html">Exception
171
    {
1814 jmachado 172
        1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest digestMd5 = 1.5.0/docs/api/java/security/MessageDigest.html">MessageDigest.getInstance("MD5");
173
        1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream fis =  new 1.5.0/docs/api/java/security/DigestInputStream.html">DigestInputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file),digestMd5);
1675 jmachado 174
 
175
        byte[] buffer = new byte[1024];
1814 jmachado 176
 
1675 jmachado 177
        int numRead;
178
 
179
        do {
180
            numRead = fis.read(buffer);
181
        } while (numRead != -1);
182
 
1814 jmachado 183
        digestMd5 = fis.getMessageDigest();
1675 jmachado 184
        fis.close();
1814 jmachado 185
        return digestMd5.digest();
1675 jmachado 186
    }
187
 
188
    // see this How-to for a faster way to convert
189
    // a byte array to a HEX string
190
    public static 1.5.0/docs/api/java/lang/String.html">String getMD5Checksum(1.5.0/docs/api/java/io/File.html">File file) throws 1.5.0/docs/api/java/lang/Exception.html">Exception {
191
        byte[] b = createChecksum(file);
192
        1.5.0/docs/api/java/lang/String.html">String result = "";
193
 
194
        for (int i=0; i < b.length; i++) {
195
            result += 1.5.0/docs/api/java/lang/Integer.html">Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
196
        }
197
        return result;
198
    }
199
 
200
    /**
201
     * Return true if files are the same in terms of bytes
202
     * @param file1
203
     * @param file2
204
     * @return
205
     */
206
    public static boolean compareTwoFiles(1.5.0/docs/api/java/io/File.html">File file1, 1.5.0/docs/api/java/io/File.html">File file2)
207
    {
208
        if(file1.length() != file2.length()){
209
            return false;
210
        }
211
 
212
        try{
213
            5+0%2Fdocs%2Fapi+InputStream">InputStream in1 =new 1.5.0/docs/api/java/io/BufferedInputStream.html">BufferedInputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file1));
214
            5+0%2Fdocs%2Fapi+InputStream">InputStream in2 =new 1.5.0/docs/api/java/io/BufferedInputStream.html">BufferedInputStream(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file2));
215
 
216
            int value1,value2;
217
            do{
218
                //since we're buffered read() isn't expensive
219
                value1 = in1.read();
220
                value2 = in2.read();
221
                if(value1 !=value2){
222
                    in1.close();
223
                    in2.close();
224
                    return false;
225
                }
226
            }while(value1 >=0);
227
 
228
            //since we already checked that the file sizes are equal
229
            //if we're here we reached the end of both files without a mismatch
230
            in1.close();
231
            in2.close();
232
            return true;
233
        } catch (1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException e) {
234
            e.printStackTrace();
235
        } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
236
            e.printStackTrace();
237
        }
238
        return false;
239
    }
240
 
1845 jmachado 241
    /**
242
     * Relative path without /
243
     * @param path
244
     * @return
245
     */
246
    public static 1.5.0/docs/api/java/io/File.html">File getFileFromRelativeContextClassLoaderPath(1.5.0/docs/api/java/lang/String.html">String path)
247
    {
248
        1.5.0/docs/api/java/io/File.html">File f;
249
        1.5.0/docs/api/java/lang/ClassLoader.html">ClassLoader classLoader = 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader();
250
 
251
        1.5.0/docs/api/java/net/URL.html">URL fileUrl = classLoader.getResource(path);
252
 
253
 
254
        try {
255
            try {
256
                f = new 1.5.0/docs/api/java/io/File.html">File(fileUrl.toURI());
257
                1.5.0/docs/api/java/lang/System.html">System.out.println(f.toString());
258
                1.5.0/docs/api/java/lang/System.html">System.out.println(f.getAbsolutePath());
259
                return f;
260
            } catch (1.5.0/docs/api/java/net/URISyntaxException.html">URISyntaxException ex) {
261
                logger.error(ex,ex);
262
                return null;
263
            } catch (1.5.0/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException ex) {
264
 
265
 
266
                try {
267
                    1.5.0/docs/api/java/lang/System.html">System.out.println("Decoding from fileUrl.getFile");
268
                    f = new 1.5.0/docs/api/java/io/File.html">File(1.5.0/docs/api/java/net/URLDecoder.html">URLDecoder.decode(fileUrl.getFile(), "UTF-8"));
269
                    1.5.0/docs/api/java/lang/System.html">System.out.println(f.getAbsolutePath());
270
                    return f;
271
                } catch (1.5.0/docs/api/java/lang/Exception.html">Exception ex2) {
272
                    logger.error(ex2,ex2);
273
                }
274
            }
275
        }catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
276
            throw new 1.5.0/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException("The properties file "
277
                    + fileUrl.toExternalForm() + " could not be loaded.", e);
278
        }
279
        return null;
280
    }
281
 
1675 jmachado 282
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/lang/Exception.html">Exception {
283
        1.5.0/docs/api/java/lang/System.html">System.out.println(getMD5Checksum(new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar.bz2")));
284
 
285
        1.5.0/docs/api/java/lang/System.html">System.out.println(getMD5Checksum(new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar.bz2")));
286
        1.5.0/docs/api/java/lang/System.html">System.out.println(getMD5Checksum(new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar.bz2")));
287
        1.5.0/docs/api/java/lang/System.html">System.out.println(compareTwoFiles(new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar.bz2"), new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar-copia.bz2")));
288
        1.5.0/docs/api/java/lang/System.html">System.out.println(compareTwoFiles(new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.0_macosx-x86.app.tar.bz2"), new 1.5.0/docs/api/java/io/File.html">File("/Volumes/Home/jorgemachado/Documents/FileZilla_3.24.1_macosx-x86.app.tar.bz2")));
289
    }
207 jmachado 290
}