Dream------Java--ant zip 对压缩文件进行指定位置的修改
ant zip 对压缩文件进行指定位置的修改
实现功能:
对2中文件进行修改:
需求:
在XX文件中,从二进制流的200字节位置开始,往后的30位字节数量.插入一个值
由于涉及到公司内部,不方便写太多。
代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; /**
* add by wls 2015年8月26日14:16:22
* @author wls
*
*
*/
public class FXCopyrightProtectionUtil { //解析odfx格式
public static void copyright_ODFX(String zipPath, String uuid) throws IOException {
String[] path = zipPath.split("\\.");
String newPath="";
//拿到去掉后缀名的路径+文件名,为重命名做准备
if(path.length==2){
newPath = path[0];
}
File zipFile = new File(zipPath);
if(zipFile!=null){
String zfName = zipFile.getName();
System.out.println(zipFile.getName());
String[] zfArray = zfName.split("\\.");
if(zfArray.length>0){
if("xx".equals(zfArray[1])){
if (!zipFile.exists())
throw new RuntimeException("zip file " + zipPath
+ " does not exist.");
zipFile.renameTo(new File(newPath+"副本."+zfArray[1]));
File newFile = new File(newPath+"副本."+zfArray[1]);
System.out.println("newFile="+newFile.getName());
File dFile = new File(zipPath);
ZipFile zipf = new ZipFile(newFile, "gbk");
ZipOutputStream zipOutputStream = new ZipOutputStream(dFile);
zipOutputStream.setEncoding("gbk");
Enumeration<ZipEntry> entries = zipf.getEntries();
while (entries.hasMoreElements()){
// 从压缩文件中获取一个条目
ZipEntry entry = entries.nextElement();
zipOutputStream.putNextEntry(entry);
if(!entry.isDirectory()){
// 获得该条目对象的数据流
InputStream is = zipf.getInputStream(entry);
System.out.println("---entry.getName"+entry.getName());
byte[] buff = new byte[199];
byte[] buff2 = new byte[2048];
int len = 0;
int a = 1;
while((len = is.read(buff)) != -1){
if(a==1){
if ((len = is.read(buff)) != -1) {
//fos.write(buff, 0, len);
zipOutputStream.write(buff,0, len);
//zipOutputStream.write("c63a5b08-7c22-47df-825d-9725194c0714".getBytes(),0,36);
zipOutputStream.write(uuid.getBytes(),0,30);
a++;
//System.out.println("a =1 --len="+len);
} }else{
if ((len = is.read(buff2)) != -1) {
//fos.write(buff, 0, len);
zipOutputStream.write(buff2,0, len);
//System.out.println("len="+len);
}
}
}
is.close();
//zipOutputStream.finish();
zipOutputStream.close();
zipOutputStream.closeEntry();
}
}
}
}
}
} //解析ftpx格式
public static void copyright_FTPX(String zipPath,String uuid) throws IOException{
String[] path = zipPath.split("\\.");
String newPath="";
//拿到去掉后缀名的路径+文件名,为重命名做准备
if(path.length==2){
newPath = path[0];
}
File zipFile = new File(zipPath);
if(zipFile!=null){
String zfName = zipFile.getName();
System.out.println(zipFile.getName());
String[] zfArray = zfName.split("\\.");
if(zfArray.length>0){
if("xxx".equals(zfArray[1])){
if (!zipFile.exists())
throw new RuntimeException("zip file " + zipPath
+ " does not exist.");
//重命名
zipFile.renameTo(new File(newPath+"副本."+zfArray[1]));
File newFile = new File(newPath+"副本."+zfArray[1]);
System.out.println("newFile="+newFile.getName());
File dFile = new File(zipPath);
ZipFile zipf = new ZipFile(newFile, "gbk");
ZipOutputStream zipOutputStream = new ZipOutputStream(dFile);
zipOutputStream.setEncoding("gbk");
Enumeration<ZipEntry> entries = zipf.getEntries();
while (entries.hasMoreElements()){
// 从压缩文件中获取一个条目
ZipEntry entry = entries.nextElement();
zipOutputStream.putNextEntry(entry);
// 获得该条目对象的数据流
InputStream is = zipf.getInputStream(entry);
System.out.println("name="+entry.getName());
if(!entry.isDirectory()){
if("zzz/zzz.key".equals(entry.getName())){
byte[] buff = new byte[128];
int len = 0;
int a = 1;
while((len = is.read(buff)) != -1){
if(a==1){
zipOutputStream.write(uuid.getBytes(),0,30);
zipOutputStream.write(buff,0, len);
a++;
//System.out.println("a =1 --len="+len);
}else{
zipOutputStream.write(buff,0, len);
//System.out.println("len="+len);
}
}
}else{
byte[] buff = new byte[2048];
int len = 0;
while((len = is.read(buff)) != -1){
zipOutputStream.write(buff,0, len);
}
}
}
is.close();
}
//System.out.println("---entry.getName"+entry.getName());
//zipOutputStream.finish();
zipOutputStream.close();
zipOutputStream.closeEntry();
}
}
}
} //解析2中格式
public static void copyright(String zipPath,String uuid) throws IOException{
String[] path = zipPath.split("\\.");
String newPath="";
//拿到去掉后缀名的路径+文件名,为重命名做准备
if(path.length==2){
newPath = path[0];
}
File zipFile = new File(zipPath);
if(zipFile!=null){
String zfName = zipFile.getName();
System.out.println(zipFile.getName());
String[] zfArray = zfName.split("\\.");
if(zfArray.length>0){
if (!zipFile.exists())
throw new RuntimeException("zip file " + zipPath
+ " does not exist.");
//重命名
zipFile.renameTo(new File(newPath+"副本."+zfArray[1]));
File newFile = new File(newPath+"副本."+zfArray[1]);
System.out.println("newFile="+newFile.getName());
File dFile = new File(zipPath);
ZipFile zipf = new ZipFile(newFile, "gbk");
ZipOutputStream zipOutputStream = new ZipOutputStream(dFile);
zipOutputStream.setEncoding("gbk");
Enumeration<ZipEntry> entries = zipf.getEntries();
while (entries.hasMoreElements()){
// 从压缩文件中获取一个条目
ZipEntry entry = entries.nextElement();
zipOutputStream.putNextEntry(entry);
// 获得该条目对象的数据流
InputStream is = zipf.getInputStream(entry);
System.out.println("name="+entry.getName());
if(!entry.isDirectory()){
if("xxx".equals(zfArray[1])){
if("docProps/auth.key".equals(entry.getName())){
byte[] buff = new byte[128];
int len = 0;
int a = 1;
while((len = is.read(buff)) != -1){
if(a==1){
zipOutputStream.write(uuid.getBytes(),0,30);
zipOutputStream.write(buff,0, len);
a++;
}else{
zipOutputStream.write(buff,0, len);
}
}
}else{
byte[] buff = new byte[2048];
int len = 0;
while((len = is.read(buff)) != -1){
zipOutputStream.write(buff,0, len);
}
}
}else if("xx".equals(zfArray[1])){
byte[] buff = new byte[199];
byte[] buff2 = new byte[2048];
int len = 0;
int a = 1;
while((len = is.read(buff)) != -1){
if(a==1){
if ((len = is.read(buff)) != -1) {
zipOutputStream.write(buff,0, len);
zipOutputStream.write(uuid.getBytes(),0,30);
a++;
} }else{
if ((len = is.read(buff2)) != -1) {
zipOutputStream.write(buff2,0, len);
}
}
}
}
}
is.close();
}
//zipOutputStream.finish();
zipOutputStream.close();
zipOutputStream.closeEntry(); }
}
} //验证
public static void myvalidate(String zipPath) throws IOException{
File zipFile = new File(zipPath);
if (!zipFile.exists())
throw new RuntimeException("zip file " + zipPath
+ " does not exist."); ZipFile zipf = new ZipFile(zipFile, "gbk");
Enumeration<ZipEntry> entries = zipf.getEntries();
while (entries.hasMoreElements()){
// 从压缩文件中获取一个条目
ZipEntry entry = entries.nextElement();
if(!entry.isDirectory()){
// 获得该条目对象的数据流
InputStream is = zipf.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(new File("C:/Users/Administrator/Desktop/myvalidate.txt"));
byte[] buff = new byte[199];
byte[] buff2 = new byte[30];
int len = 0;
int a = 1;
while(a<=2){
if(a==1){
if ((len = is.read(buff)) != -1) {
a++;
} }else{
if ((len = is.read(buff2)) != -1) {
//fos.write(buff, 0, len);
fos.write(buff2,0, len);
System.out.println("len="+len);
}
a++;
}
}
is.close();
fos.close();
//zipOutputStream.finish();
}
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String dir = new String("C:/Users/Administrator/Desktop/wlsxxx.zip");
String dir2 = new String("C:/Users/Administrator/Desktop/wlsxx.zip");
String uuid = "c63a5b08-7c22-47df-825d-9725194c0714";
//copyright_ODFX("C:/Users/Administrator/Desktop/示例素材.xx", uuid);
//copyright_FTPX("C:/Users/Administrator/Desktop/示例模板.xxx",uuid);
copyright("C:/Users/Administrator/Desktop/示例模板.xxx",uuid);
copyright("C:/Users/Administrator/Desktop/示例素材.xx", uuid);
//myvalidate(dir);
} }
Dream------Java--ant zip 对压缩文件进行指定位置的修改的更多相关文章
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- mac系统中怎么打开rar/zip等压缩文件?
平常使用mac的同学们,可能经常要接受下别人发过来的rar文件,可惜的时mac os x系统默认是不能打开rar文件(不知道以后苹果会支持rar不?),那么我们该如何去解圧rar文件,接下来我将介绍. ...
- java 提取目录下所有子目录的文件到指定位置
package folder; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExcept ...
- Java 过滤所有html标签,复制文件到指定位置
public static String filterHtml(String string) { String str = string.replaceAll("<[a-zA-Z]+[ ...
- shell如何在指定文件的指定位置后面添加内容
最近工作中遇到一个问题,想在某个文件的指定位置后面添加一个标志位,要求在shell脚本里实现. 问题说明: 想在sys_config.fex文本的某个字符串后面添加一个flag 例如:sys_conf ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- 使用java API进行zip递归压缩文件夹以及解压
一.概述 在本篇文章中,给大家介绍一下如何将文件进行zip压缩以及如何对zip包解压.所有这些都是使用Java提供的核心库java.util.zip来实现的. 二.压缩文件 首先我们来学习一个简单的例 ...
- Java用Zip进行压缩
这个总结源于Java编程思想第四版18.11节的案例: 完整代码地址: Java编程思想:压缩 相关Api地址: ZipStream ZipEntry ZipFile 进行压缩时: 1.创建Check ...
随机推荐
- Delphi : keydown与keypress的区别,组合键
Shift 是一个集合变量. type TShiftState = set of (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDoubl ...
- 【jQuery插件】pagepiling滚屏插件使用
pagePiling.js 这款 jQuery 插件可以帮助前端开发人员轻松实现全屏滚动效果. 支持所有的主流浏览器,包括IE8+,支持移动设备.下面详细讲解下pagePiling.js的使用步骤. ...
- 【Python】Python基础
源程序文件通常以.py为扩展名 #!/usr/bin/python shebang,即执行脚本时通知内容要启动的解释器 import platform 导入模块 print platform.unam ...
- HDU 6184 Counting Stars
Problem Description Little A is an astronomy lover, and he has found that the sky was so beautiful!S ...
- CodeForces - 988D(思维STL)
原文地址:https://blog.csdn.net/weixin_39453270/article/details/80548442 博主已经讲的很好了 题意: 从一个序列中,选出一个集合,使得集合 ...
- 【BZOJ5311/CF321E】贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性)
[BZOJ5311/CF321E]贞鱼/Ciel and Gondolas(动态规划,凸优化,决策单调性) 题面 BZOJ CF 洛谷 辣鸡BZOJ卡常数!!!!!! 辣鸡BZOJ卡常数!!!!!! ...
- 【转】嵌入式Linux驱动面试题三道
题一: Linux设备中字符设备与块设备有什么主要的区别? 字符设备:字符设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程序来实现这种特性.字符设备驱动程序通常至少实现open,cl ...
- 解题:BZOJ 2673 World Final 2011 Chips Challenge
题面 数据范围看起来很像网络流诶(滚那 因为限制多而且强,数据范围也不大,我们考虑不直接求答案,而是转化为判定问题 可以发现第二个限制相对好满足,我们直接枚举这个限制就可以.具体来说是枚举所有行中的最 ...
- Luogu P3251 [JLOI2012]时间流逝 期望dp
题面 题面 题解 期望\(dp\)好题! 今年\(ZJOI\)有讲过这题... 首先因为\(T\)只有\(50\),大力\(dfs\)后发现,可能的状态数最多只有\(20w\)左右,所以我们就可以大力 ...
- 「转」python数字图像处理(18):高级形态学处理
python数字图像处理(18):高级形态学处理 形态学处理,除了最基本的膨胀.腐蚀.开/闭运算.黑/白帽处理外,还有一些更高级的运用,如凸包,连通区域标记,删除小块区域等. 1.凸包 凸包是指一 ...