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 对压缩文件进行指定位置的修改的更多相关文章

  1. Java对zip格式压缩和解压缩

    Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...

  2. mac系统中怎么打开rar/zip等压缩文件?

    平常使用mac的同学们,可能经常要接受下别人发过来的rar文件,可惜的时mac os x系统默认是不能打开rar文件(不知道以后苹果会支持rar不?),那么我们该如何去解圧rar文件,接下来我将介绍. ...

  3. java 提取目录下所有子目录的文件到指定位置

    package folder; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundExcept ...

  4. Java 过滤所有html标签,复制文件到指定位置

    public static String filterHtml(String string) { String str = string.replaceAll("<[a-zA-Z]+[ ...

  5. shell如何在指定文件的指定位置后面添加内容

    最近工作中遇到一个问题,想在某个文件的指定位置后面添加一个标志位,要求在shell脚本里实现. 问题说明: 想在sys_config.fex文本的某个字符串后面添加一个flag 例如:sys_conf ...

  6. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  7. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  8. 使用java API进行zip递归压缩文件夹以及解压

    一.概述 在本篇文章中,给大家介绍一下如何将文件进行zip压缩以及如何对zip包解压.所有这些都是使用Java提供的核心库java.util.zip来实现的. 二.压缩文件 首先我们来学习一个简单的例 ...

  9. Java用Zip进行压缩

    这个总结源于Java编程思想第四版18.11节的案例: 完整代码地址: Java编程思想:压缩 相关Api地址: ZipStream ZipEntry ZipFile 进行压缩时: 1.创建Check ...

随机推荐

  1. Python日记——nginx+Gunicorn部署你的Flask项目

    转自:http://blog.csdn.net/qq_32198277/article/details/52432890 大家在学习Python的时候常常都是输入 python 文件名.py   这样 ...

  2. 【uoj#143】[UER #5]万圣节的数列 构造

    题目描述 给出一个的数列,将其重新排列,使得其等差子序列的数目最小.输出一种可能的排列后的数列. 题解 构造 那天和 EdwardFrog 讨论 bzoj2124 的构造时突然有的灵感,最后发现就是这 ...

  3. python模拟浏览器爬取数据

    爬虫新手大坑:爬取数据的时候一定要设置header伪装成浏览器!!!! 在爬取某财经网站数据时由于没有设置Header信息,直接被封掉了ip 后来设置了Accept.Connection.User-A ...

  4. 【刷题】BZOJ 5091 [Lydsy1711月赛]摘苹果

    Description 小Q的工作是采摘花园里的苹果.在花园中有n棵苹果树以及m条双向道路,苹果树编号依次为1到n,每条道路的两端连接着两棵不同的苹果树.假设第i棵苹果树连接着d_i条道路.小Q将会按 ...

  5. 【POJ2411】Mondriaan's Dream(轮廓线DP)

    [POJ2411]Mondriaan's Dream(轮廓线DP) 题面 Vjudge 题解 这题我会大力状压!!! 时间复杂度大概是\(O(2^{2n}n^2)\),设\(f[i][S]\)表示当前 ...

  6. keepalived回顾

    Keepalived是lvs的扩展项目,因此它们之间具备良好的兼容性. 通过对服务器池对象的健康检查,实现对失效机器/服务的故障隔离: 负载均衡器之间的失败切换failover,通过VRRPv2 st ...

  7. laravel Collection mapToDictionary 例子

    源码 示例 <?php require __DIR__ . '/bootstrap/app.php'; $arr = [ [ 'name' => 'John', 'age' => 2 ...

  8. shell 循环语句

    1.while 2.for 3.until 4.select while #!/bin/bash # 显示一系列数字 count=1 while [ $count -le 6 ]; do echo $ ...

  9. GO_01:Linux-CentOS之Go语言环境配置

    1.下载安装 下载当前最新版本,通过wget命令,当然你也可以手动下载之后再传入到Linux中,都可以. 本文安装的是1.8版本.由于Go语言的要求,需要配置gopath,而对应再gopath里按照g ...

  10. tensorflow变量作用域(variable scope)

    举例说明 TensorFlow中的变量一般就是模型的参数.当模型复杂的时候共享变量会无比复杂. 官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望 ...