zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }
  • 相关阅读:
    Java对象初始化
    some tool collections
    链接、单选按钮虚线框与focus:this.blur()与outline
    火狐拓展开发 基础知识
    Multidimensional Array And an Array of Arrays
    Byte Array to Hexadecimal String
    Comparer<T> IComparer<T> IComparable<T>
    Little Puzzlers–List All Anagrams in a Word
    JavaScript-Curry
    OOP in JS Public/Private Variables and Methods
  • 原文地址:https://www.cnblogs.com/wangliansong/p/4761202.html
Copyright © 2011-2022 走看看