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 ...
随机推荐
- nginx通过配置empty_gif解决请求favicon 404的问题
背景介绍 因为一些浏览器在访问网站时会默认去请求网站的favicon,但是我的网站(Tengine)上并没有这些icon图片,因此在访问日志里会出现大量的404错误,会触发一些没必要日志告警.我们可以 ...
- python2 python3共存解决方案
作者:匿名用户链接:https://www.zhihu.com/question/21653286/answer/95532074来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- c++11 闭包的实现
c++11 闭包的实现 什么是闭包 闭包有很多种定义,一种说法是,闭包是带有上下文的函数.说白了,就是有状态的函数.更直接一些,不就是个类吗?换了个名字而已. 一个函数,带上了一个状态,就变成了闭包了 ...
- AtCoder Grand Contest 004
AtCoder Grand Contest 004 A - Divide a Cuboid 翻译 给定一个\(A*B*C\)的立方体,现在要把它分成两个立方体,求出他们的最小体积差. 题解 如果有一条 ...
- 模板:快速傅里叶变换(FFT)
参考:http://blog.csdn.net/f_zyj/article/details/76037583 如果公式炸了请去我的csdn博客:http://blog.csdn.net/luyouqi ...
- bzoj1026windy数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 Description windy定义了一种windy数.不含前导零且相邻两个数字之 ...
- com.android.support:appcompat-v7 版本号问题
supportLibVersion 的头数字是和targetSdkVersion 版本一样的. ext { supportLibVersion = '22.2.1'} compile "co ...
- 2018.10.20 2018-2019 ICPC,NEERC,Southern Subregional Contest(Online Mirror, ACM-ICPC Rules)
i207M的“怕不是一个小时就要弃疗的flag”并没有生效,这次居然写到了最后,好评=.= 然而可能是退役前和i207M的最后一场比赛了TAT 不过打得真的好爽啊QAQ 最终结果: 看见那几个罚时没, ...
- OS X 安装pyspider
pyspider安装的过程中,需要安装pycurl.有几个坑 一.首先遇到权限的问题 因为/Library目录是root权限,所以非root用户对该目录的读写经常会遇到权限问题,但是不宜切换成root ...
- Django JSON 时间
在views.py中导入: from django.core.serializers.json import DjangoJSONEncoder 在返回JSON数据时调用: return HttpRe ...