apache 압축 라이브러리를 이용한 압축 클래스
라이브러리 :
============================================================================================================================================ZipUtil.java
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.util.ArrayList;
import java.util.Enumeration;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipFile;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class ZipUtil {
public static final int BUFFER_SIZE = 2048;
public static final String ENCODING_KR = "EUC-KR";
public static boolean isImageFile(String fileName){
if(fileName == null){
return false;
}
String lowerName = fileName.toLowerCase();
if(lowerName.endsWith(".jpg") || lowerName.endsWith(".jpeg") || lowerName.endsWith(".png")){
return true;
}
return false;
}
public static void zipMoneybook(ArrayList<String> files, String zipFile) throws IOException {
if((files==null) || (files.size() < 1) || (zipFile==null)){
return;
}
ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(zipFile));
zipout.setLevel(5);
zipout.setComment("Ocsoosoo Zip");
try {
ZipEntry ze=null;
String imagememodirname = Util.EXPORT_IMAGEMEMO_DIRNAME;
imagememodirname = imagememodirname.endsWith("/") ? imagememodirname : imagememodirname + "/";
ze = new ZipEntry(imagememodirname);
zipout.putNextEntry(ze);
for (int i = 0; i < files.size(); i++) {
String filePath = files.get(i);
if(isImageFile(filePath)){
BufferedInputStream origin = null;
FileInputStream fi = new FileInputStream(filePath);
origin = new BufferedInputStream(fi, BUFFER_SIZE);
byte data[] = new byte[BUFFER_SIZE];
try {
String imageZipPath = imagememodirname+filePath.substring(filePath.lastIndexOf("/")+1);
ZipEntry entry = new ZipEntry(imageZipPath);
zipout.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER_SIZE)) >= 0) {
zipout.write(data, 0, count);
}
}
finally {
origin.close();
}
}else{
addZipEntry(new File(filePath), zipout);
}
}
}
finally {
zipout.close();
}
}
public static void zip(ArrayList<String> files, String zipFile) throws IOException {
if((files==null) || (files.size() < 1) || (zipFile==null)){
return;
}
ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(zipFile));
zipout.setLevel(5);
zipout.setComment("Ocsoosoo Zip");
try {
for (int i = 0; i < files.size(); i++) {
String filePath = files.get(i);
addZipEntry(new File(filePath), zipout);
}
}
finally {
zipout.close();
}
}
public static boolean zip(String zipDirectoryOrFile, String saveZipfilePath) throws IOException {
if((zipDirectoryOrFile == null) || (saveZipfilePath == null)){
return false;
}
File zipEntryFile = new File(zipDirectoryOrFile);
if((zipEntryFile == null) || (!zipEntryFile.isDirectory() && !zipEntryFile.isFile() && !zipEntryFile.canRead())){
return false;
}
File zipfile = new File(saveZipfilePath);
OutputStream out = new FileOutputStream(zipfile);
ZipOutputStream zipout = null;
try {
zipout = new ZipOutputStream(out);
zipout.setLevel(5);
zipout.setComment("Ocsoosoo Zip");
addZipEntry(zipEntryFile, zipout);
}catch(Exception e){
e.printStackTrace();
}finally{
zipout.close();
}
return true;
}
public static void addZipEntry(File entryFile, ZipOutputStream zipOut){
if((entryFile == null) || !entryFile.exists() || !entryFile.canRead() ||(zipOut == null)){
return;
}
try{
ZipEntry ze=null;
if(entryFile.isDirectory()){
String name = /*entryFile.getParent()+"/"+*/entryFile.getName();
if (File.separatorChar != '/'){
name = name.replace(File.separatorChar, '/');
}
name = name.endsWith("/") ? name : name + "/";
ze = new ZipEntry(name);
ze.setTime(entryFile.lastModified());
zipOut.putNextEntry(ze);
File [] listFile = entryFile.listFiles();
if((listFile!=null) && (listFile.length > 0)){
for(File entry : listFile){
try{
addZipEntry(entry, zipOut);
}catch(Exception e){
e.printStackTrace();
}
}
}
}else if(entryFile.isFile()){
ze = new ZipEntry(entryFile.getName());
ze.setTime(entryFile.lastModified());
zipOut.putNextEntry(ze);
copy(entryFile, zipOut);
zipOut.closeEntry();
}
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static boolean isIncludedFile(String zipFilePath, String searchFileName) {
ZipFile zipFile = null;
boolean fileFound = false;
if((zipFilePath==null) || (searchFileName==null)){
return false;
}
try {
//zipFile = new ZipFile(zipFilePath); //java.util.zip
//Enumeration<? extends ZipEntry> e = zipFile.entries();//java.util.zip
zipFile = new ZipFile(zipFilePath, ENCODING_KR);//org.apache.tools.zip
Enumeration<? extends ZipEntry> e = zipFile.getEntries(); //org.apache.tools.zip
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry)e.nextElement();
String entryName = entry.getName();
if (entryName.equalsIgnoreCase(searchFileName)) {
fileFound = true;
break;
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return fileFound;
}
@SuppressWarnings("unchecked")
public static boolean isIncludedFileType(String zipFilePath, String searchFileType) {
ZipFile zipFile = null;
boolean fileFound = false;
if((zipFilePath==null) || (searchFileType==null)){
return false;
}
try {
//zipFile = new ZipFile(zipFilePath); //java.util.zip
//Enumeration<? extends ZipEntry> e = zipFile.entries();//java.util.zip
zipFile = new ZipFile(zipFilePath, ENCODING_KR);//org.apache.tools.zip
Enumeration<? extends ZipEntry> e = zipFile.getEntries(); //org.apache.tools.zip
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
String entryName = entry.getName();
if (entryName.toLowerCase().endsWith(searchFileType.toLowerCase())) {
fileFound = true;
break;
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return fileFound;
}
@SuppressWarnings("unchecked")
public static ArrayList<String> getEntries(String zipFilePath) {
ArrayList<String> entriesList = new ArrayList<String>();
entriesList.clear();
ZipFile zipFile = null;
try {
//zipFile = new ZipFile(zipFilePath); //java.util.zip
//Enumeration<? extends ZipEntry> e = zipFile.entries();//java.util.zip
zipFile = new ZipFile(zipFilePath, ENCODING_KR);//org.apache.tools.zip
Enumeration<? extends ZipEntry> e = zipFile.getEntries(); //org.apache.tools.zip
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
entriesList.add(entry.getName());
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return entriesList;
}
@SuppressWarnings("unchecked")
public static void unzip(String zipFilePath, String unZipLocation) throws IOException {
int size;
byte[] buffer = new byte[BUFFER_SIZE];
ZipFile zipFile = null;
Enumeration<? extends ZipEntry> entries = null;
if ((zipFilePath == null) || (unZipLocation == null)) {
return;
}
try {
if (!unZipLocation.endsWith("/")) {
unZipLocation += "/";
}
File f = new File(unZipLocation);
if (f != null) {
if (f.exists() && !f.isDirectory()) {
f.delete();
}
f.mkdirs();
}
try {
// zipFile = new ZipFile(compressedZipFile);//java.util.zip
// entries = zfile.entries();//java.util.zip
zipFile = new ZipFile(zipFilePath, ENCODING_KR);// org.apache.tools.zip
entries = zipFile.getEntries(); // org.apache.tools.zip
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String path = unZipLocation + entry.getName();
if (entry.isDirectory()) {
File unzipFile = new File(path);
if (!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
FileOutputStream out = new FileOutputStream(path, false);
BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
InputStream in = zipFile.getInputStream(entry);
try {
copy(in, fout);
} finally {
in.close();
fout.flush();
fout.close();
}
}
}
} finally {
try {
if (zipFile != null){
zipFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static boolean unZip(File compressedFile) {
Enumeration<? extends ZipEntry> entries;
ZipFile zipFile = null;
if(compressedFile==null){
return false;
}
try {
zipFile = new ZipFile(compressedFile, ENCODING_KR);
String extractDirName = compressedFile.getParent() + File.separator+Util.getFileBodyName(compressedFile.getName());
(new File(extractDirName)).mkdir();
entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String entryFileName = entry.getName();
if (entry.isDirectory()) {
(new File(extractDirName, entryFileName)).mkdir();
} else {
String fileDir = Util.getDirectory(entryFileName);
if(fileDir != null){
File dirFile = new File(fileDir);
if(!dirFile.exists() || (dirFile.exists() && dirFile.isFile())){
dirFile.mkdirs();
}
}
copy(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(extractDirName + entryFileName)));
}
}
} catch (ZipException ze) {
ze.printStackTrace();
return false;
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
} finally {
try {
if (zipFile != null){
zipFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
int readCount = in.read(buffer);
if (readCount < 0) {
break;
}
out.write(buffer, 0, readCount);
}
}
private static void copy(File file, OutputStream out) throws IOException {
InputStream in = new FileInputStream(file);
try {
copy(in, out);
} finally {
in.close();
}
}
private static void copy(InputStream in, File file) throws IOException {
OutputStream out = new FileOutputStream(file);
try {
copy(in, out);
} finally {
out.close();
}
}
}
============================================================================================================================================
'programmer > android' 카테고리의 다른 글
selector setting (0) | 2013.06.11 |
---|---|
Dialog softkeyboard (0) | 2013.06.10 |
HorizontalListView (0) | 2013.05.28 |
onTouch와 onTouchEvent (0) | 2013.02.18 |
PendingIntent (1) | 2012.12.14 |