java 中压缩和解压文件的方法

来源:互联网 发布:知乎 经略幽燕我童贯 编辑:程序博客网 时间:2024/04/26 07:26

调用ant.jar里的方法可以完好的实现包括中文在内的zip的压缩与解压

 

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.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
*
@author Every E-mail/MSN:mwgjkf@hotmail.com
*               QQ:30130942
*
@version 创建时间:Feb 26, 2009 6:01:11 PM
* 类说明:解压文件
*
*/
public class Decompression {
   
private static final int BUFFEREDSIZE = 1024;
   
public Decompression() {
       
// TODO Auto-generated constructor stub
    }
   /**
     * 解压zip格式的压缩文件
     *
     *
@param zipFileName
     *            压缩文件
     *
@param extPlace
     *            解压目录
     *
@throws Exception
    
*/
   
public static void unzip(String zipFileName, String extPlace)
           
throws Exception {
       
try {
            ZipFile zipFile
= new ZipFile(zipFileName);
            Enumeration e
= zipFile.getEntries();
            ZipEntry zipEntry
= null;
           
while (e.hasMoreElements()) {
                zipEntry
= (ZipEntry) e.nextElement();
                String entryName
= zipEntry.getName();
                String names[]
= entryName.split("/");
               
int length = names.length;
                String path
= extPlace + File.separator;
               
for (int v = 0; v < length; v++) {
                   
if (v < length - 1) {
                        path
+= names[v] + "/";
                        System.out.println(path);
                       
new File(path).mkdirs();
                    }
else { // 最后一个
                        if (entryName.endsWith("/")) { // 为目录,则创建文件夹
                            new File(extPlace + File.separator + entryName).mkdirs();
                        }
else {
                            InputStream in
= zipFile.getInputStream(zipEntry);
                            File outentity
= new File(extPlace+ File.separator +entryName);
                           
if(outentity.exists()){
                                outentity.createNewFile();
                            }
                            OutputStream os
= new FileOutputStream(outentity);
                           
byte[] buf = new byte[BUFFEREDSIZE];
                           
int len;
                           
while ((len = in.read(buf)) > 0) {
                                os.write(buf,
0, len);
                            }
                            in.close();
                            os.close();
                        }
                    }
                }
            }
            zipFile.close();
        }
catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 压缩zip格式的压缩文件
     *
@param inputFilename 压缩的文件或文件夹及详细路径
     *
@param zipFilename 输出文件名称及详细路径
     *
@throws IOException
    
*/
   
public synchronized void zip(String inputFilename, String zipFilename) throws IOException {   
        zip(
new File(inputFilename), zipFilename);   
    }   
   
   
/**
     * 压缩zip格式的压缩文件
     *
@param inputFile 需压缩文件
     *
@param zipFilename 输出文件及详细路径
     *
@throws IOException
    
*/
   
public synchronized void zip(File inputFile, String zipFilename) throws IOException {   
        ZipOutputStream out
= new ZipOutputStream(new FileOutputStream(zipFilename));
       
try {   
            zip(inputFile, out,
"");   
        }
catch (IOException e) {   
           
throw e;   
        }
finally {   
            out.close();   
        }   
    }
   
/**
     * 压缩zip格式的压缩文件
     *
@param inputFile 需压缩文件
     *
@param out 输出压缩文件
     *
@param base 结束标识
     *
@throws IOException
    
*/
    @SuppressWarnings(
"unused")
   
private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
       
if (inputFile.isDirectory()) {   
            File[] inputFiles
= inputFile.listFiles();   
            out.putNextEntry(
new ZipEntry(base + "/"));   
            base
= base.length() == 0 ? "" : base + "/";   
           
for (int i = 0; i < inputFiles.length; i++) {   
                zip(inputFiles[i], out, base
+ inputFiles[i].getName());   
            }
        }
else {   
           
if (base.length() > 0) {   
                out.putNextEntry(
new ZipEntry(base));   
            }
else {   
                out.putNextEntry(
new ZipEntry(inputFile.getName()));   
            }
            FileInputStream in
= new FileInputStream(inputFile);   
           
try {   
               
int c;   
               
byte[] by = new byte[BUFFEREDSIZE];   
               
while ((c = in.read(by)) != -1) {   
                    out.write(by,
0, c);   
                }   
            }
catch (IOException e) {   
               
throw e;   
            }
finally {
                in.close();   
            }   
        }   
    }

}

原创粉丝点击