Skip to content

Instantly share code, notes, and snippets.

@SocraticPhoenix
Created July 22, 2015 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SocraticPhoenix/ac38ff15911377a704a3 to your computer and use it in GitHub Desktop.
Save SocraticPhoenix/ac38ff15911377a704a3 to your computer and use it in GitHub Desktop.
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 neonblue858@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.qixalite.plugins.meguy26.api.file;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.qixalite.plugins.meguy26.api.map.ListUtil;
/**
* The class containing methods useful to File manipulation.
*
* @author Meguy26
*
*/
public class FileUtil {
/**
* Creates a new FileUtil instance.
*/
public FileUtil() {
}
/**
* Deletes all files in the specified directory, as well as the directory itself. If the file is not a directory, it is simply deleted.
* @param dir The directory to delete.
*/
public void deleteDirectory(String dir){
this.deleteDirectory(new File(dir));
}
/**
* Deletes all files in the specified directory, as well as the directory itself. If the file is not a directory, it is simply deleted.
* @param dir The directory to delete.
*/
public void deleteDirectory(File dir){
if(dir.isDirectory()){
for(File f : dir.listFiles()){
this.deleteDirectory(f);
}
}
dir.delete();
}
/**
* Lists all the files in a directory, including the directory itself, all the files in it, all sub directories, and files in each sub directory.
* @param dir The directory to search.
* @return A list of all files discovered.
*/
public List<File> getAllFilesInDirectory(String dir){
return this.getAllFilesInDirectory(new File(dir));
}
/**
* Lists all the files in a directory, including the directory itself, all the files in it, all sub directories, and files in each sub directory.
* @param dir The directory to search.
* @return A list of all files discovered.
*/
public List<File> getAllFilesInDirectory(File dir){
List<File> files = new ArrayList<File>();
files.add(dir);
if(dir.isDirectory()){
this.loopThroughDir(dir, files);
}
return ListUtil.reverseList(files);
}
private void loopThroughDir(File dir, List<File> holder){
if(dir.isDirectory()){
for(File f : dir.listFiles()){
holder.add(f);
if(f.isDirectory()){
this.loopThroughDir(f, holder);
}
}
}
}
/**
* Downloads a file from the url, and writes it to the filename. Please note
* that the url must be a direct download link and the filename may be a
* path.
*
* @param url
* the direct download link used to access the file.
* @param filename
* the path to download the file to.
* @throws MalformedURLException When the URL is malformed
* @throws IOException When the download was interrupted somehow
*/
public void downloadFile(String url, String filename)
throws MalformedURLException, IOException {
BufferedInputStream in = null;
FileOutputStream fout = null;
try {
in = new BufferedInputStream(new URL(url).openStream());
fout = new FileOutputStream(filename);
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} finally {
if (in != null) {
in.close();
}
if (fout != null) {
fout.close();
}
}
}
/**
* Unzips a zip file and writes it to the output folder.
*
* @param zipFileName
* the path to the zip file.
* @param outputFolder
* the path to unzip the file into.
* @throws IOException When the unzip process failed
*/
public void unZipFile(String zipFileName, String outputFolder)
throws IOException {
File dir = new File(outputFolder);
if (!dir.exists() || !dir.isDirectory()) {
dir.mkdir();
}
// Open the zip file
ZipFile zipFile = new ZipFile(zipFileName);
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = zipEntry.getName();
// Do we need to create a directory ?
File file = new File(outputFolder + "\\" + name);
if (name.endsWith("/")) {
file.mkdirs();
continue;
}
File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}
// Extract the file
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
is.close();
fos.close();
}
zipFile.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment