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 ...
随机推荐
- poj3061 Subsequence(尺取)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- Latex编译过程中遇到的奇奇怪怪的问题及解决方案
标签(空格分隔): 杂七杂八的问题 有必要写一个博文记录自己在Latex编译时遇到的各种问题,希望可以帮到遇到同样错误的亲故.讲真,一直没有系统的学习Latex,都是投哪个会直接拿那个会的模板来套,然 ...
- cookie属性和作用
面试面到了cookie,自己只是还是有一点欠缺,找到一篇文章,学习一下 在chrome控制台中的resources选项卡中可以看到cookie的信息. 现在的chrome控制台已经更新了,所以要到Ap ...
- [洛谷P4340][SHOI2016]随机序列
题目大意:有$n(n\leqslant10^5)$个数,每两个数之间可以加入$+-\times$三种符号,$q(q\leqslant10^5)$次询问,每次询问修改一个数后,所有表达式可能的值的和 题 ...
- Android Support WorkManager使用详解
使用WorkManager调度任务 WorkManager是一个库, 用以将工作入队, 当该工作的约束条件得到满足之后, WorkManager保证它的执行. WorkManager允许观测工作的状态 ...
- 编译安装haproxy开启支持SSL
1.下载程序包 # wget http://www.haproxy.org/download/1.7/src/haproxy-1.7.5.tar.gz # tar xvf haproxy-1.7.5. ...
- 18华南理工校赛 K 小马哥的超级盐水
https://www.nowcoder.com/acm/contest/94/K sum(ai)/sum(bi) = x/y <=> sum(ai*yi-bi*x) = 0 跟这题有点类 ...
- Chapter 5(串)
1.kmp #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <str ...
- Docker Swarm应用--lnmp部署WordPress
一.简介 目的:使用Docker Swarm 搭建lnmp来部署WordPress 使用Dockerfile构建nginx.php镜像 将构建的镜像上传docker私有仓库 使用volume做work ...
- Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法
Hadoop基础-HDFS递归列出文件系统-FileStatus与listFiles两种方法 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. fs.listFiles方法,返回Loc ...