java修改压缩文件_Dream------Java--ant zip 对压缩文件进行指定位置的修改

这是一个Java程序,使用Apache Ant的Zip工具修改压缩文件内的特定内容。它首先重命名原始文件,然后读取并修改zip文件的条目,将指定的UUID插入到特定文件的指定位置。
摘要由CSDN通过智能技术生成

importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Enumeration;importorg.apache.tools.zip.ZipEntry;importorg.apache.tools.zip.ZipFile;importorg.apache.tools.zip.ZipOutputStream;/*** add by wls 2015年8月26日14:16:22

*@authorwls

*

**/

public classFXCopyrightProtectionUtil {//解析odfx格式

public static void copyright_ODFX(String zipPath, String uuid) throwsIOException {

String[] path= zipPath.split("\\.");

String newPath="";//拿到去掉后缀名的路径+文件名,为重命名做准备

if(path.length==2){

newPath= path[0];

}

File zipFile= newFile(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= newFile(zipPath);

ZipFile zipf= new ZipFile(newFile, "gbk");

ZipOutputStream zipOutputStream= newZipOutputStream(dFile);

zipOutputStream.setEncoding("gbk");

Enumeration 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) throwsIOException{

String[] path= zipPath.split("\\.");

String newPath="";//拿到去掉后缀名的路径+文件名,为重命名做准备

if(path.length==2){

newPath= path[0];

}

File zipFile= newFile(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= newFile(zipPath);

ZipFile zipf= new ZipFile(newFile, "gbk");

ZipOutputStream zipOutputStream= newZipOutputStream(dFile);

zipOutputStream.setEncoding("gbk");

Enumeration 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) throwsIOException{

String[] path= zipPath.split("\\.");

String newPath="";//拿到去掉后缀名的路径+文件名,为重命名做准备

if(path.length==2){

newPath= path[0];

}

File zipFile= newFile(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= newFile(zipPath);

ZipFile zipf= new ZipFile(newFile, "gbk");

ZipOutputStream zipOutputStream= newZipOutputStream(dFile);

zipOutputStream.setEncoding("gbk");

Enumeration 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) throwsIOException{

File zipFile= newFile(zipPath);if (!zipFile.exists())throw new RuntimeException("zip file " +zipPath+ " does not exist.");

ZipFile zipf= new ZipFile(zipFile, "gbk");

Enumeration 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) throwsException {//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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值