2012-09-20 03:19:12 +02:00
|
|
|
/**
|
2014-10-24 01:14:48 +02:00
|
|
|
* Copyright 2014 Ryszard Wiśniewski <brut.alll@gmail.com>
|
2012-09-20 03:19:12 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package brut.util;
|
|
|
|
|
|
|
|
import java.io.*;
|
2015-03-25 17:56:55 +01:00
|
|
|
import java.util.zip.CRC32;
|
2015-04-16 15:12:20 +02:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
import java.util.zip.ZipOutputStream;
|
2015-03-25 17:56:55 +01:00
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
|
|
|
|
*/
|
|
|
|
public class BrutIO {
|
|
|
|
public static void copyAndClose(InputStream in, OutputStream out)
|
|
|
|
throws IOException {
|
|
|
|
try {
|
|
|
|
IOUtils.copy(in, out);
|
|
|
|
} finally {
|
2015-03-30 07:30:52 +02:00
|
|
|
IOUtils.closeQuietly(in);
|
|
|
|
IOUtils.closeQuietly(out);
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long recursiveModifiedTime(File[] files) {
|
|
|
|
long modified = 0;
|
|
|
|
for (int i = 0; i < files.length; i++) {
|
|
|
|
long submodified = recursiveModifiedTime(files[i]);
|
|
|
|
if (submodified > modified) {
|
|
|
|
modified = submodified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long recursiveModifiedTime(File file) {
|
|
|
|
long modified = file.lastModified();
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
File[] subfiles = file.listFiles();
|
|
|
|
for (int i = 0; i < subfiles.length; i++) {
|
|
|
|
long submodified = recursiveModifiedTime(subfiles[i]);
|
|
|
|
if (submodified > modified) {
|
|
|
|
modified = submodified;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modified;
|
|
|
|
}
|
2015-03-25 17:56:55 +01:00
|
|
|
|
2015-03-30 07:30:52 +02:00
|
|
|
public static CRC32 calculateCrc(InputStream input) throws IOException {
|
2015-03-25 17:56:55 +01:00
|
|
|
CRC32 crc = new CRC32();
|
2015-03-30 07:30:52 +02:00
|
|
|
int bytesRead;
|
|
|
|
byte[] buffer = new byte[8192];
|
2015-03-25 17:56:55 +01:00
|
|
|
while((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
crc.update(buffer, 0, bytesRead);
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:12:20 +02:00
|
|
|
public static void copy(File inputFile, ZipOutputStream outputFile) throws IOException {
|
|
|
|
try (
|
|
|
|
FileInputStream fis = new FileInputStream(inputFile)
|
|
|
|
) {
|
|
|
|
IOUtils.copy(fis, outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void copy(ZipFile inputFile, ZipOutputStream outputFile, ZipEntry entry) throws IOException {
|
|
|
|
try (
|
|
|
|
InputStream is = inputFile.getInputStream(entry)
|
|
|
|
) {
|
|
|
|
IOUtils.copy(is, outputFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-20 03:19:12 +02:00
|
|
|
}
|