Test.java
import java.io.IOException; public class Test { public static void main(String[] args) throws IOException, Exception { String srcPath = "E:\\zhanglong.zip"; String dstPath = "E:\\zhanglong"; Decompression.unZip(srcPath, dstPath); } }
Decompression.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Decompression { //解压gzip数据 public static String unGzip(InputStream is){ String result = null; GZIPInputStream in = null; ByteArrayOutputStream arrayOutputStream = null; try { in = new GZIPInputStream(is); arrayOutputStream = new ByteArrayOutputStream(); int len = -1; byte[] buffer = new byte[Constant.BUFFER]; while ((len = in.read(buffer)) != -1) { arrayOutputStream.write(buffer, 0, len); } result = new String(arrayOutputStream.toByteArray(), Constant.UTF); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } if(arrayOutputStream != null){ arrayOutputStream.close(); } if(is != null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } //压缩gzip数据 public static byte[] gzip(String data, String charset) { byte[] b = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(data.getBytes(charset)); gzip.finish(); gzip.close(); b = bos.toByteArray(); bos.close(); } catch (Exception ex) { ex.printStackTrace(); } return b; } /**************************************************/ //解压zip压缩包 public static boolean unZip(String srcPath, String dstPath) { boolean isUnzipSuccess = false; boolean isDstFormat = false; int count = -1; int index = -1; File dstFolder = new File(dstPath); if(!dstFolder.exists()) dstFolder.mkdirs(); FileInputStream fis = null; ZipInputStream zis = null; FileOutputStream fos = null; BufferedOutputStream bos = null; try { ZipEntry entry = null; fis = new FileInputStream(srcPath); zis = new ZipInputStream(new BufferedInputStream(fis)); while ((entry = zis.getNextEntry()) != null) { byte[] data = new byte[Constant.BUFFER * 10]; String temp = entry.getName(); isDstFormat = isDstFormat(temp); if(!isDstFormat) continue; index = temp.lastIndexOf("/"); if (index > -1) temp = temp.substring(index + 1); temp = dstPath + File.separator + temp; File file = new File(temp); file.createNewFile(); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, Constant.BUFFER * 10); while ((count = zis.read(data, 0, Constant.BUFFER * 10)) != -1) { bos.write(data, 0, count); } bos.flush(); } isUnzipSuccess = true; } catch (Exception e) { e.printStackTrace(); }finally{ try { if(bos != null) bos.close(); if(fos != null) fos.close(); if(zis != null) zis.close(); if(fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); } } return isUnzipSuccess; } public static boolean isDstFormat(String filename){ boolean isDstFormat = false; boolean isEndWithOgg = filename.endsWith(Constant.OGG); boolean isEndWidthPack = filename.endsWith(Constant.PACK); boolean isEndWidthPng = filename.endsWith(Constant.PNG); boolean isEndWidthMp3 = filename.endsWith(Constant.MP3); boolean isEndWidthTmx = filename.endsWith(Constant.TMX); boolean isEndWidthProperties = filename.endsWith(Constant.PROPERTIES); if( isEndWithOgg || isEndWidthPack || isEndWidthMp3 || isEndWidthPng || isEndWidthTmx || isEndWidthProperties) isDstFormat = true; return isDstFormat; } }
FileOperate.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class FileOperate { /** * 在指定路径下创建新文件 * @param filePath 文件路径 * @return * @throws IOException */ public static File createFile(String filePath, boolean isDeleteAllFiles) { String parentPath = filePath.substring(0, filePath.lastIndexOf(File.separator)); createFolders(parentPath, isDeleteAllFiles); File file = new File(filePath); try { if(!file.createNewFile()){ file.delete(); file.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } return file; } /** * 判断配置文件是否存在 * @param filePath 文件路径 * @return */ public static boolean isFileExist(String filePath){ File file = new File(filePath); return file.exists(); } /** * 取得指定目录下的所有文件夹名 * @return */ public static List<String> getFilesName(String filePath){ List<String> files_name = null; File file = new File(filePath); if(file.exists()){ files_name = new ArrayList<String>(); File[] files = file.listFiles(); for (File file2 : files) { if(file2.isDirectory()){ files_name.add(file2.getName()); } } } return files_name; } public static void deleteFiles(File file){ if(file.exists()){ File[] files = file.listFiles(); for(File f : files){ if(f.isFile()) f.delete(); } } } public static File createFolders(String path, boolean isDeleteAllFiles){ File file = new File(path); if(file.exists() && file.isDirectory()){ if(isDeleteAllFiles) deleteFiles(file); }else{ file.mkdirs(); } return file; } public static boolean deleteFile(String filePath){ boolean isDeleted = false; File file = new File(filePath); if(file.exists()){ if(file.isFile()) { file.delete(); isDeleted = true; } } return isDeleted; } public static void deleteMkdir(String filePath){ File file = new File(filePath); if(file.exists() && file.isDirectory()){ file.delete(); } } /*********删除文件夹******/ public static boolean deleteFolders(String filePath){ boolean isDeleteSuccess = false; LinkedList<String> folderList = new LinkedList<String>(); folderList.add(filePath); while(folderList.size() > 0){ File file1 = new File(folderList.poll()); File[] files1 = file1.listFiles(); ArrayList<File> fileList = new ArrayList<File>(); for(int i = 0; i < fileList.size(); i++){ if(files1[i].isDirectory()){ folderList.add(files1[i].getPath()); }else{ fileList.add(files1[i]); } } //删除文件 for(File file : fileList){ file.delete(); } } //删除文件夹 folderList = new LinkedList<String>(); folderList.add(filePath); while(folderList.size() > 0){ File file2 = new File(folderList.getLast()); if(file2.delete()){ folderList.removeLast(); }else{ File[] files2 = file2.listFiles(); for(int i = 0; i < files2.length; i++){ folderList.add(files2[i].getPath()); } } } if(folderList.size() == 0) isDeleteSuccess = true; return isDeleteSuccess; } public static boolean moveFile(String srcFilePath, String dstFilePath){ boolean isMoveFileSuccess = false; BufferedInputStream bis = null; BufferedOutputStream bos = null; FileInputStream fis = null; FileOutputStream fos = null; try { if(isFileExist(dstFilePath)) deleteFile(dstFilePath); File dstFile = createFile(dstFilePath, false); fis = new FileInputStream(new File(srcFilePath)); fos = new FileOutputStream(dstFile); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos, Constant.BUFFER * 10); int count = -1; byte[] buffer = new byte[Constant.BUFFER * 10]; while((count = bis.read(buffer, 0, Constant.BUFFER * 10)) != -1){ bos.write(buffer, 0, count); bos.flush(); } isMoveFileSuccess = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(bos != null){ bos.close(); } if(fos != null){ fos.close(); } if(bis != null){ bis.close(); } if(fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } } return isMoveFileSuccess; } }
Constant.java
public class Constant { public static final int BUFFER = 1024; public static final String UTF = "utf-8"; public static final String OGG = ".ogg"; public static final String PACK = ".pack"; public static final String PNG = ".png"; public static final String MP3 = ".mp3"; public static final String PROPERTIES = ".properties"; public static final String TMX = ".tmx"; }
测试包:
http://download.csdn.net/detail/zlqqhs/5943973