All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.imqa.injector.util.FileUtil Maven / Gradle / Ivy
package io.imqa.injector.util;
import org.apache.tools.ant.taskdefs.condition.Os;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class FileUtil {
/**
* 압축풀기 메소드
* @param zipFileName 압축파일
* @param directory 압축 풀 폴더
*/
public static void decompress(String zipFileName, String directory) throws Throwable {
File zipFile = new File(zipFileName);
FileInputStream fis = null;
ZipInputStream zis = null;
ZipEntry zipentry = null;
try {
//파일 스트림
fis = new FileInputStream(zipFile);
//Zip 파일 스트림
zis = new ZipInputStream(fis);
//entry가 없을때까지 뽑기
while ((zipentry = zis.getNextEntry()) != null) {
String filename = zipentry.getName();
File file = new File(directory, filename);
//entiry가 폴더면 폴더 생성
if (zipentry.isDirectory()) {
file.mkdirs();
} else {
//파일이면 파일 만들기
createFile(file, zis);
}
}
} catch (Throwable e) {
throw e;
} finally {
if (zis != null)
zis.closeEntry();
if (fis != null)
fis.close();
}
}
/**
* 파일 만들기 메소드
* @param file 파일
* @param zis Zip스트림
*/
private static void createFile(File file, ZipInputStream zis) throws Throwable {
//디렉토리 확인
File parentDir = new File(file.getParent());
//디렉토리가 없으면 생성하자
if (!parentDir.exists()) {
parentDir.mkdirs();
}
//파일 스트림 선언
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
byte[] buffer = new byte[256];
int size = 0;
//Zip스트림으로부터 byte뽑아내기
while ((size = zis.read(buffer)) > 0) {
//byte로 파일 만들기
fos.write(buffer, 0, size);
}
} catch (Throwable e) {
throw e;
} finally {
if (fos != null)
fos.close();
}
}
/**
* 압축 메소드
* @param path 경로
* @param outputFileName 출력파일명
*/
public static void compress(String path,String outputFileName) throws Throwable{
if (path.contains("\\"))
path= path.replace("\\", "/");
File file = new File(path);
int pos = outputFileName.lastIndexOf(".");
if(!outputFileName.substring(pos).equalsIgnoreCase(".zip")){
outputFileName += ".zip";
}
// 압축 경로 체크
if(!file.exists()){
throw new Exception("Not File!");
}
// 출력 스트림
FileOutputStream fos = null;
// 압축 스트림
ZipOutputStream zos = null;
try{
fos = new FileOutputStream(new File(outputFileName));
zos = new ZipOutputStream(fos);
// 디렉토리 검색
searchDirectory(file,zos);
}catch(Throwable e){
throw e;
}finally{
if(zos != null) zos.close();
if(fos != null) fos.close();
}
}
public static void compress2(String path, String outputFileName) throws Throwable {
if (path.contains("\\"))
path= path.replace("\\", "/");
File file = new File(path);
int pos = outputFileName.lastIndexOf(".");
if(!outputFileName.substring(pos).equalsIgnoreCase(".zip")){
outputFileName += ".zip";
}
// 압축 경로 체크
if(!file.exists()){
throw new Exception("Not File!");
}
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files == null) return;
multiCompress(files, outputFileName);
} else {
singleCompress(file.getAbsolutePath(), outputFileName);
}
}
public static void singleCompress(String srcFile, String outputFileName) throws IOException {
FileOutputStream fos = new FileOutputStream(outputFileName);
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(srcFile);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
public static void multiCompress(File[] srcFiles, String outputFileName) throws IOException{
// List srcFiles = Arrays.asList("test1.txt", "test2.txt");
FileOutputStream fos = new FileOutputStream(outputFileName);
ZipOutputStream zipOut = new ZipOutputStream(fos);
for (File fileToZip : srcFiles) {
if (fileToZip.isDirectory())
zipFile(fileToZip, fileToZip.getName(), zipOut);
else {
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
}
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
if (fileName.endsWith("/")) {
zipOut.putNextEntry(new ZipEntry(fileName));
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new ZipEntry(fileName + "/"));
zipOut.closeEntry();
}
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
fis.close();
}
/**
* 다형성
*/
private static void searchDirectory(File file, ZipOutputStream zos) throws Throwable{
searchDirectory(file,file.getPath(),zos);
}
/**
* 디렉토리 탐색
* @param file 현재 파일
* @param root 루트 경로
* @param zos 압축 스트림
*/
private static void searchDirectory(File file,String root,ZipOutputStream zos)
throws Exception{
//지정된 파일이 디렉토리인지 파일인지 검색
if(file.isDirectory()){
//디렉토리일 경우 재탐색(재귀)
File[] files = file.listFiles();
for(File f : files){
searchDirectory(f,root,zos);
}
}else{
//파일일 경우 압축을 한다.
compressZip(file,root,zos);
}
}
/**
* 압축 메소드
* @param file
* @param root
* @param zos
* @throws Exception
*/
private static void compressZip(File file, String root, ZipOutputStream zos) throws Exception{
FileInputStream fis = null;
try{
String spliter = "/";
/*if (Os.isFamily(Os.FAMILY_WINDOWS)) {
spliter = "\\";
}*/
String zipName = file.getPath().replace(root+spliter, "");
// 파일을 읽어드림
fis = new FileInputStream(file);
// Zip엔트리 생성(한글 깨짐 버그)
ZipEntry zipentry = new ZipEntry(zipName);
// 스트림에 밀어넣기(자동 오픈)
zos.putNextEntry(zipentry);
int length = (int)file.length();
byte[] buffer = new byte[length];
//스트림 읽어드리기
fis.read(buffer, 0, length);
//스트림 작성
zos.write(buffer, 0, length);
}catch(Throwable e){
throw e;
}finally{
if(fis != null) fis.close();
}
}
public static void unzipJar(String destinationDir, String jarPath) throws IOException {
File file = new File(jarPath);
JarFile jar = new JarFile(file);
String seperator = "/";
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
seperator = "\\";
}
// fist get all directories,
// then make those directory on the destination Path
for (Enumeration enums = jar.entries(); enums.hasMoreElements();) {
JarEntry entry = (JarEntry) enums.nextElement();
String fileName = destinationDir + File.separator + entry.getName();
fileName = fileName.replace("/", seperator);
File f = new File(fileName);
if (fileName.endsWith(seperator)) {
f.mkdirs();
}
}
//now create all files
for (Enumeration enums = jar.entries(); enums.hasMoreElements();) {
JarEntry entry = (JarEntry) enums.nextElement();
String fileName = destinationDir + File.separator + entry.getName();
fileName = fileName.replace("/", seperator);
// Logger.d("JAR fileName", fileName);
File f = new File(fileName);
if (!fileName.endsWith(seperator)) {
InputStream is = jar.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f);
// write contents of 'is' to 'fos'
while (is.available() > 0) {
fos.write(is.read());
}
fos.close();
is.close();
}
}
jar.close();
}
public static String execCmd(String cmd) throws java.io.IOException {
StringBuilder result = new StringBuilder("");
/*
Scanner s = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream());//.useDelimiter("\\A");
StringBuilder output = new StringBuilder("");
while(s.hasNext()) {
output.append(s.next());
}
return output.toString();*/
// Process proc = Runtime.getRuntime().exec(cmd.split(" "));
Process proc = Runtime.getRuntime().exec(cmd.split(" "));
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// read the output from the command
String s = null;
while ((s = stdInput.readLine()) != null) {
result.append(s);
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
result.append(s);
}
try {
stdInput.close();
stdError.close();
proc.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result.toString();
}
public static boolean deleteDirectory(String path) {
return deleteDirectory(new File(path));
}
public static boolean deleteDirectory(File path) {
if(!path.exists()) {
return false;
}
if (path.isFile()) {
return path.delete();
}
File[] files = path.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
if (!file.delete()) {
// Logger.d("File Delete", "Fail at : "+file.getAbsolutePath());
}
}
}
}
return path.delete();
}
public static boolean moveDirectory(File path) {
if(!path.exists()) {
return false;
}
File[] files = path.listFiles();
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
return path.delete();
}
}