Show FileUtil.java syntax highlighted
package fr.loria.ecoo.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
*
* @author nabil
*/
public class FileUtil {
public static final int BUFFER = 2048;
/** Creates a new instance of FileUtil */
public FileUtil() {
}
// public static void zipDirectory(String dirToCompress, String
// destinationDirectory) {
// try {
// // create a ZipOutputStream to zip the data to
// ZipOutputStream zos = new ZipOutputStream(new
// FileOutputStream(destinationDirectory));
// // Set the compression ratio
// zos.setLevel(Deflater.BEST_COMPRESSION);
// // create a new File object based on the directory we have to zip
// // File
// File zipDir = new File(dirToCompress);
// // get a listing of the directory content
// String[] dirList = zipDir.list();
// byte[] readBuffer = new byte[2156];
// int bytesIn = 0;
// // loop through dirList, and zip the files
// for (int i = 0; i < dirList.length; i++) {
// File f = new File(zipDir, dirList[i]);
// if (!f.isDirectory()) {
// // create a FileInputStream on top of f
// FileInputStream fis = new FileInputStream(f);
// // create a new zip entry
// ZipEntry anEntry = new ZipEntry(f.getPath());
// // place the zip entry in the ZipOutputStream object
// zos.putNextEntry(anEntry);
// // now write the content of the file to the ZipOutputStream
// while ((bytesIn = fis.read(readBuffer)) != -1) {
// zos.write(readBuffer, 0, bytesIn);
// }
// // close the Stream
// fis.close();
// }
//
// }
// // close the stream
// zos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public static void zipDirectory(String pathSource, String PathDestination, String nomFichier) throws Exception {
FileOutputStream dest = new FileOutputStream(PathDestination + File.separator + nomFichier);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
add(pathSource, "", out);
out.close();
}
/**
* parcour de l'arborscence [le path de la racine est "" ]
*
* @param pathSource
* String
* @param path
* String
* @param out
* ZipOutputStream
*/
private static void add(String pathSource, String path, ZipOutputStream out) throws Exception {
File f = new File(pathSource + File.separator + path);
File files[] = f.listFiles();
BufferedInputStream origin = null;
byte data[] = new byte[BUFFER];
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
add(pathSource, path + File.separator + files[i].getName() + File.separator, out);
} else {
FileInputStream fi = new FileInputStream(pathSource + path + File.separator + files[i].getName());
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(path + files[i].getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
}
}
public static void unzipDirectory(String fileToDecompress, String destinationDirectory) throws Exception {
Enumeration entries;
destinationDirectory = destinationDirectory + File.separator;
ZipFile zipFile = new ZipFile(fileToDecompress);
entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String fileName = entry.getName();
fileName = fileName.replaceAll("\\\\", File.separator);
File fEntry = new File(fileName);
String parent = fEntry.getParent();
if (parent != null && parent.trim().length() > 0) {
(new File(destinationDirectory + File.separator + parent)).mkdir();
}
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(destinationDirectory + File.separator + fEntry)));
}
zipFile.close();
}
private static void copyInputStream(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
// public static void unzipDirectory(String dirToDecompress,
// String destinationDirectory) throws Exception
// {
// // try
// // {
// File sourceZipFile = new File(dirToDecompress);
// File unzipDestinationDirectory = new File(destinationDirectory);
// // Open Zip file for reading
// ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
//
// // Create an enumeration of the entries in the zip file
// Enumeration zipFileEntries = zipFile.entries();
//
// // Process each entry
// while (zipFileEntries.hasMoreElements())
// {
// // grab a zip file entry
// ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
// // unzip all files at the root of destinationDirectory without
// // recreating arborescence
// String currentEntry = (new File(entry.getName())).getName();
// System.out.println("Extracting: " + entry);
// File destFile = new File(unzipDestinationDirectory,
// currentEntry);
//
// // grab file's parent directory structure
// File destinationParent = destFile.getParentFile();
//
// // extract file if not a directory
// if (!entry.isDirectory())
// {
// BufferedInputStream is = new BufferedInputStream(zipFile
// .getInputStream(entry));
// int currentByte;
// // establish buffer for writing file
// byte data[] = new byte[2048];
//
// // write the current file to disk
// FileOutputStream fos = new FileOutputStream(destinationParent +
// File.separator + destFile);
// BufferedOutputStream dest = new BufferedOutputStream(fos,
// 2048);
//
// // read and write until last byte is encountered
// while ((currentByte = is.read(data, 0, 2048)) != -1)
// {
// dest.write(data, 0, currentByte);
// }
// dest.flush();
// dest.close();
// is.close();
// } else
// {
// new File ((zipFile.getInputStream(entry)).toString()).mkdir();
// }
// }
// zipFile.close();
//
// // }
// // catch (IOException ioe)
// // {
// // ioe.printStackTrace();
// // }
//
// }
public static void copyFiles(String strPath, String dstPath) throws IOException {
//
// File src = new File(strPath);
// File dest = new File(dstPath);
//
// if (src.isDirectory())
// {
// if (copySubDir)
// {
// dest.mkdirs();
// String list[] = src.list();
//
// for (int i = 0; i < list.length; i++)
// {
// String dest1 = dest.getAbsolutePath() + File.separator
// + list[i];
// String src1 = src.getAbsolutePath() + File.separator + list[i];
// copyFiles(src1, dest1, copySubDir);
// }
//
// } else
// {
// // do nothing
// }
//
// } else
// {
// FileChannel sourceChannel = new FileInputStream(src).getChannel();
// FileChannel targetChannel = new FileOutputStream(dest).getChannel();
// sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
// sourceChannel.close();
// targetChannel.close();
//
// }
File sourceDir = new File(strPath);
for (File file : sourceDir.listFiles()) {
if (file.isFile() && file.canRead()) {
FileUtil.copyFile(file.toString(), dstPath + File.separator + file.getName());
}
}
}
public static void copyFile(String fileName, String dstPath) throws IOException {
FileChannel sourceChannel = new FileInputStream(fileName).getChannel();
FileChannel destinationChannel = new FileOutputStream(dstPath).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
sourceChannel.close();
destinationChannel.close();
}
public static void delDir(File dir) {
if (dir.exists()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
dir.delete();
}
}
}
See more files for this project here