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 ...
随机推荐
- java异常处理-finally中使用return和throw语句
java异常语句中的finally块通常用来做资源释放操作,如关闭文件.关闭网络连接.关闭数据库连接等.正常情况下finally语句中不应该使用return语句也不应该抛出异常,以下讨论仅限于java ...
- UVALive6442_Coins on a Ring
真正的水题,可惜无法当场机智一下. 这样的,在一个圈圈上给你n个黑点,现在要你移动每一个黑点使得所有的点都是等间距的,每个点中最远需要一定的那个点最小可以是多少? 其实是这样来考虑的,我们可以随便设置 ...
- 模板:插头dp
前言: 严格来讲有关dp的都不应该叫做模板,因为dp太活了,但是一是为了整理插头dp的知识,二是插头dp有良好的套路性,所以姑且还叫做模板吧. 这里先推荐一波CDQ的论文和这篇博客http://www ...
- BZOJ 1014 火星人 | 平衡树维护哈希
BZOJ 1014 火星人 题意 有一个字符串,三中操作:在某位置后面插入一个字符.修改某位置的字符.询问两个后缀的最长公共前缀. 题解 看到网上的dalao们都说这道题是平衡树,我就很懵x--平衡树 ...
- java随机数的有趣用法
直接用代码说明,比较容易理解 package com.wz.other; import java.util.Random;import java.util.concurrent.ThreadLocal ...
- Android Fragment 替代方案
refs: Square 开源库Flow和Mortar的介绍https://github.com/hehonghui/android-tech-frontier/tree/master/android ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- python调用powershell、远程执行bat
python调用本地powershell方法 1.现在准备一个简陋的powershell脚本,功能是测试一个IP列表哪些可以ping通: function test_ping($iplist) { f ...
- nginx启用stream日志配置文件
主配置文件/etc/nginx/nginx.conf增加内容: stream { log_format proxy '$remote_addr [$time_local] ' '$protocol $ ...
- Linux Wget 命令实例讲解
Linux wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件或从远程服务器恢复备份到本地服务器.如果我们使用虚拟主机,处理这样的 ...