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. 微軟将从 .NET 4 以后的版本弃用 System.Data.OracleClient 以及Oracle 的各种连接方法

    这是微软官方 ADO.NET Team Blog 去年就公布的消息: http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracl ...

  2. 【原】iOS学习44之动画

    1. 简单动画 1> UIImageView GIF 动画 GIF图的原理是:获取图片,存储在图片数组中,按照图片数组的顺序将图片以一定的速度播放 UIImageView *showGifima ...

  3. soapui中文操作手册(十)----REST Sample Project

    第一步:打开MockService 1.双击MockService: 2.单击开始mockservice. 你会看到mockservice“端口8080上运行”. 参考: 配置使用MockServic ...

  4. UVa 11082 & 最大流的行列模型

    题意: 给出一个矩阵前i行的和与前j列的和,(i∈[1,r],j属于[1,c]),每个元素ai,j∈[1,20],请你还原出这个矩阵,保证有解. SOL: 给网络流建模跪了,神一样的建图,如果我我会怎 ...

  5. 21045308刘昊阳 《Java程序设计》第十周学习总结

    21045308刘昊阳 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 狭义的网络编程范畴:程序员所作的事情 ...

  6. Leetcode Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  7. Coder-Strike 2014 - Finals (online edition, Div. 2) B. Start Up

    需要满足的条件是 (1)每个字母是对称的 (2)每个字符串是对称的 #include <iostream> #include <algorithm> #include < ...

  8. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

  9. 让 cell 显示底部线条时,总是有几个线条被隐藏.

    一,经历 1> 感觉像是重用的问题,但从代码的分析中找不出任何问题. 2> 感觉像是我 在创建怎样的 cell 的代码 被 layoutsubviews 方法覆盖了一样.于是先在创建怎样的 ...

  10. 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)

        华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...