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);
}
}
java向压缩文件添加文件的更多相关文章
- java如何压缩多个文件到压缩包,并下载到浏览器?
java压缩多个文件到压缩包,并下载到浏览器 解决方法: 完整的方法如下,很简单,亲试有效,极力推荐. 我是以流作为文件,而不是file,循环把所有pdf文件压缩到pdf.zip压缩包中. 1.前 ...
- SVN的搭建及使用(三)用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突,重新设置用户名和密码等
添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态,如图: 这时候,你需要告知TortoiseSVN你的操作,如图: 加入以后,你的文件会变 ...
- VS2010在C#头文件添加文件注释的方法(转)
步骤: 1.VS2010 中找到(安装盘符以C盘为例)C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCa ...
- java生成压缩文件
在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...
- java压缩文件或文件夹并导出
java压缩文件或文件夹并导出 tozipUtil: package com.zhl.push.Utils; import java.io.File; import java.io.FileInput ...
- 【转】Java实现将文件或者文件夹压缩成zip
转自:https://www.cnblogs.com/zeng1994/p/7862288.html package com.guo.utils; import java.io.*; import j ...
- Java—将文件压缩为zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- Java生成压缩文件(zip、rar 格式)
jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
随机推荐
- [bzoj2118]墨墨的等式【dijk+堆】
10/30的update:如果是冲着dijk的板子来的,建议看多校联考contest中第二场day2的T2,那边的写法比较优秀... --------------------------------- ...
- [转]JavaScript跨域总结与解决办法
转载自http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html仅用作个人读书笔记. 什么是跨域 1.document.domain+ ...
- jQuery入门第二天
3种选择器:元素选择器:$("button").class选择器:$(".btn").id选择器:$("#target1"). <sc ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Problem to create "New Database Diagram" in Microsoft SQL Server Management Studio for SQL Server 2012
Error: when click "New Database Diagram", a error popped up and said "Attempted to re ...
- BZOJ2164 : 采矿
树链剖分+线段树,每个节点维护以下信息: (1)单独在某个点分配$i$个人的最大收益.可以$O(m)$合并. (2)分配$i$个人的最大收益.可以用$O(m^2)$合并. 时间复杂度$O(c(m^2\ ...
- Node.js的线程和进程
http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本章的学习,能够 ...
- Redis的两个小技巧
1.通配删除相同前缀的缓存 DEL命令的参数不支持通配符,但我们可以结合Linux的管道和xargs命令自己实现删除所有符合规则的键. 比如要删除所有以“structure_”开头的键,就可以执行下面 ...
- 洛谷 P1144 最短路计数 Label:水
题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M,为图的顶点数与边数. 接下来M行 ...
- js动态创建的元素绑定事件
新创建的元素用传统的办法无法绑定,需要用live方法. 例: $('.rule').live('mouseover', function () { $(this).addClass("can ...