ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码

Java 对zip文件中含有中文子文件操作

来源:网络整理     时间:2015-05-28     关键词:

本篇文章主要介绍了"Java 对zip文件中含有中文子文件操作",主要涉及到方面的内容,对于Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下: 小鱼仔做文件压缩解压的时候,开始并没有注意细节,使用的java jdk中zip操作工具类这样导致的问题是对中文操作都是乱码,要么就是破损。查阅资料才知道这个实在...

小鱼仔做文件压缩解压的时候,开始并没有注意细节,使用的java jdk中zip操作工具类

这样导致的问题是对中文操作都是乱码,要么就是破损。

查阅资料才知道这个实在1.6的历史遗留问题

还好开源的apache 提供了一个支持包,让我们拿过来就可以用。

下面是下载链接

提供jar包下载链接:http://download.csdn.net/detail/u010962482/8748091

废话不多说 贴上解压压缩代码。

 /**
     * 解压文件到指定目录
     * @param zipFile
     * @param descDir
     * @author isea533
     */  
    @SuppressWarnings( "rawtypes") 
    public static void unZipFiles(String zip_s_File,String descDir){
     OutputStream out= null;
     InputStream in = null;
     File zipFile= new File(zip_s_File);

        //判断解压路径是否存在,不存在则创建
     File pathFile = new File(descDir); 
        if(!pathFile.exists()){ 
            pathFile.mkdirs(); 
        }
        //创建待解压文件夹路径
        ZipFile zip;
           try {
		//注意 这里的编码 按自己情况自己填写
              zip = new ZipFile(zipFile,"gbk" );
               for(Enumeration   entries = zip.getEntries();entries.hasMoreElements();){ 
                 ZipEntry entry = (ZipEntry)entries.nextElement(); 
                 String zipEntryName = entry.getName(); 
                 in = zip.getInputStream(entry); 
                 String outPath = (descDir+zipEntryName).replaceAll("\\*" , "/" );; 
                 //输出文件路径信息  ,如果存在文件则覆盖,如果不想覆盖修改OUTputStream的Boolean类型
                 System. out.println(outPath); 
                  out = new FileOutputStream(outPath,true ); 
                 byte[] buf1 = new byte[1024]; 
                 int len; 
                 while((len=in.read(buf1))>0){ 
                     out.write(buf1,0,len); 
                 } 

                 }
               System. out.println("******************解压完毕********************" );       
          } catch (IOException e) {
              System. out.println("解压文件失败" );
              e.printStackTrace();
          } finally{
               try {
                   in.close(); 
                 out.close(); 
              } catch (Exception e2) {
                   System. out.println("管理io流失败" );
                   e2.printStackTrace();
              }
          }

    } 

    /**
    *
    * @param sourceFilePath   需要压缩 zip 路径
    * @param zipFilePath      zip压缩文件路径
    * @param fileName         zip 名字
    * @return
    */
   public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
      boolean flag = false ;
      File sourceFile = new File(sourceFilePath);
      FileInputStream fis = null;
      BufferedInputStream bis = null;
      FileOutputStream fos = null;
      ZipOutputStream zos = null;

      if(sourceFile.exists() == false){
          System. out.println("待压缩的文件目录:" +sourceFilePath+"不存在." );
      } else{
              File zipFile = new File(zipFilePath + "/" + fileName +".zip" );

          try {       if(zipFile.exists()){
                  System. out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" + "打包文件." );
              } else{
                  File[] sourceFiles = sourceFile.listFiles();
                  if(null == sourceFiles || sourceFiles.length<1){
                      System. out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩." );
                  } else{
                      fos = new FileOutputStream(zipFile);
                      zos = new ZipOutputStream(new BufferedOutputStream(fos));
                      zos.setEncoding( "gbk");
                      byte[] bufs = new byte[1024*10];
                      for(int i=0;i

以上就介绍了Java 对zip文件中含有中文子文件操作,包括了方面的内容,希望对Javajrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_138251.html

相关图片

相关文章