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);
}
}

java向压缩文件添加文件的更多相关文章

  1. java如何压缩多个文件到压缩包,并下载到浏览器?

    java压缩多个文件到压缩包,并下载到浏览器   解决方法: 完整的方法如下,很简单,亲试有效,极力推荐. 我是以流作为文件,而不是file,循环把所有pdf文件压缩到pdf.zip压缩包中. 1.前 ...

  2. SVN的搭建及使用(三)用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突,重新设置用户名和密码等

    添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态,如图: 这时候,你需要告知TortoiseSVN你的操作,如图: 加入以后,你的文件会变 ...

  3. VS2010在C#头文件添加文件注释的方法(转)

    步骤: 1.VS2010 中找到(安装盘符以C盘为例)C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCa ...

  4. java生成压缩文件

    在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...

  5. java压缩文件或文件夹并导出

    java压缩文件或文件夹并导出 tozipUtil: package com.zhl.push.Utils; import java.io.File; import java.io.FileInput ...

  6. 【转】Java实现将文件或者文件夹压缩成zip

    转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...

  7. Java—将文件压缩为zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  8. Java生成压缩文件(zip、rar 格式)

    jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...

  9. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

随机推荐

  1. js兼容方法:事件添加|事件绑定|事件监听 addEvent

    function addEvent(obj,sEvent,fn){ if(obj.attachEvent){ obj.attachEvent("on"+sEvent,fn); }e ...

  2. Thymeleaf 集成spring

    Thymeleaf 集成spring 如需先了解Thymeleaf的单独使用,请参考<Thymeleaf模板引擎使用>一文. 依赖的jar包 Thymeleaf 已经集成了spring的3 ...

  3. iOS学习37数据处理之CoreData

    1. CoreData数据库框架的优势 1> CoreData历史 CoreData数据持久化框架是Cocoa API 的一部分,首次在iOS5版本的系统中出现,它允许按照实体-属性-值模型组织 ...

  4. 【转】ssh登录慢,等待输入密码时间长的解决办法

    http://youhuiba.net/2013/06/09/520.html 有时候在ssh远程登录到其他主机上时发现登录时间太长,要等待很久才会出现输入密码的提示,google了一下,发现主要有两 ...

  5. HDU - Hotel

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

  6. asp.net mvc route 中新发现的小技巧

    在发现这个小技巧之前,我经常被某些问题困扰,我们以博客园为例 1:是分类名称 2:是分类url 3:点击分类,进入的页面,要显示分类的名称 4:点击分类,进入的页面,要用分类相关参数 在日常web的开 ...

  7. MathType的公式在word中跟文字不对齐

    引用http://blog.sina.com.cn/s/blog_4d1f40c00100net8.html 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公 ...

  8. Linux下bash: scp: command not found问题 或者装ssh包时报错 Requires: libedit.so.0()(64bit)

        一.用scp命令从物理主机向CentOS 6.1虚拟机传送文件,提示以下错误:bash: scp: command not found到CentOS 6.1虚拟机查看也缺少scp命令.该虚拟机 ...

  9. 20145330Java程序设计第三次实验

    20145330<Java程序设计>第三次实验报告 实验三 敏捷开发与XP实践 实验内容 1.使用git上传代码 2.使用git实现代码开发实践 3.实现代码的重载 实验步骤 使用git上 ...

  10. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...