将一个文件“我的生命.jpg”用winzip压缩入文件mylife.zip。
然后用以下代码读取:
String inFilename = "f:\\mylife.zip";
String zipEntryName = null;
ZipFile zf = new ZipFile(inFilename);
// Enumerate each entry
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) 
// Get the entry name
{
zipEntryName = ((ZipEntry)entries.nextElement()).getName();

System.out.println(zipEntryName);
}
结果输出的zipEntryName是乱码,试了用getByte设置成GB2312,GBK等都不行,请问如何解决呢?

解决方案 »

  1.   

    中文名称的文件打包后的乱码问题,或者说如何将中文的文件打包
    将文件系统中的几个中文名称的文件打包后,zip包的名字是乱码,这个问题不好解决。在网上也没有找到彻底解决的办法。有个比较使用的做法是首先将文件的名称进行编码,比如编码为十六进制的形式,然后打包,解压缩包的时候再对文件名称解码,就得到中文的文件名了。
      

  2.   

    试试这个.import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    import sun.io.*;public class linzip
    {
     public static String make8859toGB(String str)
     {
      try{
       String str8859 = new String(str.getBytes("8859_1"),"GB2312");
       return str8859;
      }catch(UnsupportedEncodingException ioe){
       return str;
      }
     }
     
     public static void main(String[] args)
     {
      if (args.length < 1){
       System.err.println("Required parameter missing!");
       System.exit(-1);
      }
      
      
      File infile = new File(args[0]);  try{
       //检查是否是ZIP文件
       ZipFile zip = new ZipFile(infile);
       zip.close();
       
       //建立与目标文件的输入连接
       ZipInputStream in = new ZipInputStream(new FileInputStream(infile));
       ZipEntry file = in.getNextEntry();
       
         
       int i =infile.getAbsolutePath().lastIndexOf(''.'');
       String dirname = new String();
       if ( i != -1 )
        dirname = infile.getAbsolutePath().substring(0,i);
       else
        dirname = infile.getAbsolutePath();
       File newdir = new File(dirname);
       newdir.mkdir();
       
       byte[] c = new byte[1024];
       int len;
       int slen;
       
       while (file != null){
        
        i = make8859toGB(file.getName()).replace(''/'',''\\'').lastIndexOf(''\\'');
        if ( i != -1 ){
         File dirs = new File(dirname+File.separator+make8859toGB(file.getName()).replace(''/'',''\\'').substring(0,i));
         dirs.mkdirs();
         dirs = null;
        }
        
        System.out.print("Extract "+make8859toGB(file.getName()).replace(''/'',''\\'')+" ........  ");
        
        if (file.isDirectory()){
         File dirs = new File(make8859toGB(file.getName()).replace(''/'',''\\''));
         dirs.mkdir();
         dirs = null;
        }
        else{ 
         FileOutputStream out = new FileOutputStream(dirname+File.separator+make8859toGB(file.getName()).replace(''/'',''\\''));
         while((slen = in.read(c,0,c.length)) != -1)
          out.write(c,0,slen);
         out.close();
        }
        System.out.print("O.K.\n");
        file = in.getNextEntry();
       }
       in.close();
      }catch(ZipException zipe){
       MessageBox(0,infile.getName()+"不是一个ZIP文件!","文件格式错误",16);
      }catch(IOException ioe){
       MessageBox(0,"读取"+args[0]+"时错误!","文件读取错误",16);
      }catch(Exception i){
        System.out.println("over");
      }
     } /**
      * @dll.import("USER32", auto) 
      */
     public static native int MessageBox(int hWnd, String lpText, String lpCaption, int uType);
    }
      

  3.   


    org.apache.tool.ant.zip.*
    其中有
    ZipOutputStream 中有setencoding 方法
      

  4.   

    刚才打错了是org.apache.tool.zip.*