java向压缩文件添加文件

package org.alfresco.repo.bom.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.Deflater;

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

public class AppendFileToCompressedFileUtil {
    
    private static final String compressedFilePath = "F:/chiang.zip";
    private static final String newCompressedFilePath = "F:/tdp.zip";
    private static final String appendFilePackage = "data";
    
    public void append(String appendFile) throws Exception{
        ZipOutputStream zos = null;
        InputStream input = null;
        File newCompressedFile = new File(newCompressedFilePath); 
        if (newCompressedFile.exists()) { 
            newCompressedFile.delete(); 
        }
        try {
            ZipFile compressedFile = new ZipFile(compressedFilePath, "GBK");
            //System.out.println(compressedFile.getEncoding());
            zos = new ZipOutputStream(new FileOutputStream(newCompressedFilePath));
            zos.setEncoding("GBK");
            zos.setComment("Bale tdp!");
            zos.setLevel(Deflater.BEST_COMPRESSION);
            zos.setMethod(Deflater.DEFLATED);
            //
            if (!"".equals(appendFile)) {
                File f = new File(appendFile);
                ZipEntry pag = new ZipEntry(appendFilePackage+f.separator);
                zos.putNextEntry(pag);
                ZipEntry fileEntry = new ZipEntry(appendFilePackage+f.separator+f.getName());
                zos.putNextEntry(fileEntry);
                input = new FileInputStream(f);
                startCopy(zos, input);
            }
            Enumeration<? extends ZipEntry> e = compressedFile.getEntries();
            while (e.hasMoreElements()) {
                ZipEntry entry = e.nextElement();
                zos.putNextEntry(entry);
                if (!entry.isDirectory()) {
                    startCopy(zos, compressedFile.getInputStream(entry));
                }
                zos.closeEntry();
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(zos!=null)
                zos.close();
        }
    }
    
    public void startCopy(ZipOutputStream zos,InputStream input) throws Exception{
        int data = 0 ;
        try {
            while ((data=input.read())!=-1) {
                zos.write(data);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(input!=null)
                input.close();
        }
    }
    // main test method
    public static void main(String[] args) throws Exception{
        AppendFileToCompressedFileUtil a = new AppendFileToCompressedFileUtil();
        String append = "F:/你现在好吗.txt";
        a.append(append);
    }
}
 
 

 

转载于:https://www.cnblogs.com/chiangfai/p/5828190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值