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);
}
}