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,那边的写法比较优秀... --------------------------------- ...
- Jenkins基础 - 常用配置操作
1.修改jenkins的根目录,默认地在C:\Users\用户名\.jenkins下(win7) 或C:\Documents and Settings\用户名\.jenkins(xp) 修改步骤: 增 ...
- start bbs轻论坛安装时数据库提示错误
A PHP Error was encounteredSeverity: NoticeMessage: Only variable references should be returned by r ...
- BZOJ1841 : 蚂蚁搬家
树分治,对于每个分治结构,维护两棵线段树. 第一棵按dfs序维护所有点到重心的距离,第二棵维护每个分支的最长链. 那么当前结构对答案的贡献就是第二棵线段树的最大值$+$次大值. 对于操作$0$,如果是 ...
- AngularJS学习笔记之directive—scope选项与绑定策略
From:http://www.linuxidc.com/Linux/2015-05/116924.htm scope:{}使指令与外界隔离开来,使其模板(template)处于non-inherit ...
- 移动端H5页面之iphone6的适配(转)
iphone6 及 iphone 6 plus 已经出来一段时间了.很多移动端网站,以前写死body 为320px的,现在估计也忙着做适配了. 大屏幕手机其实一直有,只是以前大家没怎么重视,移动端的H ...
- CDOJ 435 (SCOI 2011) 糖果 Label:差分约束系统
糖果 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others) Submit Stat ...
- [深入浅出Windows 10]不同平台设备的适配
2.3 不同平台设备的适配 Windows 10通用应用程序针对特定的平台还会有一个子API的集合,当我们要使用到某个平台的特定API的时候(比如手机相机硬件按钮触发事件),这时候就需要调用特定平台的 ...
- [WP8.1UI控件编程]Windows Phone XAML页面的编译
1.1.2 XAML页面的编译 Windows Phone的应用程序项目会通过Visual Studio完成XAML页面的编译,在程序运行时会通过直接链接操作加载和解析XAML,将XAML和过程式代码 ...
- 【BZOJ】3676: [Apio2014]回文串
http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题意:给一个串求回文串×出现次数的最大值.(|S|<=300000) #include ...