Java 实现解压缩文件
http://blog.chinaunix.net/uid-21209537-id-3050665.html
In this article you will learn how to unzip file.
1. Read ZIP file with “ZipInputStream”
2. Get the files to “ZipEntry” and output it to “FileOutputStream“
Decompress ZIP file example
In this example, it will read a ZIP file from zipFile, and decompress all zipped files to OUTPUT_FOLDER folder.
- public void unZipIt(String zipFile, String outputFolder){
- byte[] buffer = new byte[1024];
- try{
- //create output directory is not exists
- File folder = new File(OUTPUT_FOLDER);
- if(!folder.exists()){
- folder.mkdir();
- }
- //get the zip file content
- ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
- //get the zipped file list entry
- ZipEntry ze = zis.getNextEntry();
- while(ze!=null){
- String fileName = ze.getName();
- File newFile = new File(outputFolder + File.separator +fileName);
- System.out.println("file unzip: "+ newFile.getAbsoluteFile());
- //create all non exists folders
- //else you will hit FileNotFoundException for compressed folder
- new File(newFile.getParent()).mkdirs();
- FileOutputStream fos = new FileOutputStream(newFile);
- int len;
- while((len = zis.read(buffer)) > 0){
- fos.write(buffer,0,len);
- }
- fos.close();
- ze = zis.getNextEntry();
- }
- zis.closeEntry();
- zis.close();
- System.out.println("Extract completed.");
- }catch(IOException e){
- e.printStackTrace();
- }
- }
压缩解压完整实例:
- package org.hello.zip;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- public class AppZip {
- List<String> fileList;
- private static final String OUTPUT_ZIP_FILE = "C:\\Logs.zip";
- private static final String SOURCE_FOLDER = "C:\\Logs";
- private static final String INPUT_ZIP_FILE = "C:\\Logs.zip";
- private static final String OUTPUT_FOLDER = "C:\\outputzip";
- AppZip(){
- fileList =new ArrayList<String>();
- }
- public static void main(String[] args){
- AppZip appzip = new AppZip();
- appzip.generateFileList(new File(SOURCE_FOLDER));
- appzip.zipIt(OUTPUT_ZIP_FILE);
- appzip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER);
- }
- public void unZipIt(String zipFile, String outputFolder){
- byte[] buffer = new byte[1024];
- try{
- //create output directory is not exists
- File folder = new File(OUTPUT_FOLDER);
- if(!folder.exists()){
- folder.mkdir();
- }
- //get the zip file content
- ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
- //get the zipped file list entry
- ZipEntry ze = zis.getNextEntry();
- while(ze!=null){
- String fileName = ze.getName();
- File newFile = new File(outputFolder + File.separator +fileName);
- System.out.println("file unzip: "+ newFile.getAbsoluteFile());
- //create all non exists folders
- //else you will hit FileNotFoundException for compressed folder
- new File(newFile.getParent()).mkdirs();
- FileOutputStream fos = new FileOutputStream(newFile);
- int len;
- while((len = zis.read(buffer)) > 0){
- fos.write(buffer,0,len);
- }
- fos.close();
- ze = zis.getNextEntry();
- }
- zis.closeEntry();
- zis.close();
- System.out.println("Extract completed.");
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- public void zipIt(String zipFile) {
- byte[] buffer = new byte[1024];
- try{
- FileOutputStream fos = new FileOutputStream(zipFile);
- ZipOutputStream zos = new ZipOutputStream(fos);
- System.out.println("Output to Zip: "+ zipFile);
- for(String filename :this.fileList){
- System.out.println("File added: "+filename);
- ZipEntry ze = new ZipEntry(filename);
- zos.putNextEntry(ze);
- FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + filename);
- int len;
- while((len = in.read(buffer)) > 0){
- zos.write(buffer,0,len);
- }
- in.close();
- }
- zos.closeEntry();
- zos.close();
- System.out.println("Compress Completed.");
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- /**
- * Traverse a directory and get all files,
- * and add the file into fileList
- * @param node file or directory
- */
- public void generateFileList(File node) {
- //add file only
- if(node.isFile()){
- fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
- }
- if(node.isDirectory()){
- String[] subNote = node.list();
- for(String filename : subNote){
- generateFileList(new File(node, filename));
- }
- }
- }
- /**
- * Format the file path for zip
- * @param file file path
- * @return Formatted file path
- */
- public String generateZipEntry(String filename) {
- return filename.substring(SOURCE_FOLDER.length()+1, filename.length());
- }
- }
I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within.
I am That I am
程序 = 数据结构 + 算法