• 相关软件
    >java解zip压缩 和 把目录压缩为ZIP文件 创建者:webmaster 更新时间:2008-11-18 17:05

    使用java.util.zip解ZIP文件压缩,但不只,为何文件名不支持中文,如果那位高手知道,请告诉我,哈哈,谢谢

    String compress ="D:/doc_c06_net.zip";//zip压缩文件
    
    String decompression="D:/doc_redcome_com/";//解压路径
    
    java.io.File dir =new java.io.File(decompression);
    
    java.util.zip.ZipFile zf = new java.util.zip.ZipFile(compress);
    
    java.util.Enumeration enumer = zf.entries();
    
    while (enumer.hasMoreElements())
    {
      
        java.util.zip.ZipEntry ze = (java.util.zip.ZipEntry) enumer.nextElement();
        
        String zename = ze.getName();
        
        zename = new String(zename.getBytes("ISO-8859-1"), "UTF-8");//文件名,不支持中文.
        
        System.out.println(zename);
        
        if (ze.isDirectory())
        {
            java.io.File file = new java.io.File(dir.getAbsolutePath() + "/" + zename);
            
            file.mkdirs();
        } else
        {
            java.io.File file = new java.io.File(dir.getAbsolutePath() + "/" + zename).getParentFile();
            
            if (!file.exists())
            {
                file.mkdirs();
            }
            byte zeby[] = new byte[(int) ze.getSize()];
            
            java.io.InputStream is = zf.getInputStream(ze);
            
            is.read(zeby);
            
            is.close();
            
            java.io.FileOutputStream fos = new java.io.FileOutputStream(
    
    dir.getAbsolutePath() + "/" + zename); fos.write(zeby); fos.close(); } } zf.close();

    使用java.util.zip,把目录压缩为ZIP文件

    public static void main(String args[])
    {
      String compress ="D:/doc_c06_net/";//要压缩的目录
      
      String decompression="D:/doc_redcome_com/";//解压路径
      
      java.util.zip.ZipOutputStream zos =  new java.util.zip.ZipOutputStream(
    new java.io.FileOutputStream(
    
    >new java.io.File(decompression))); compress(zos,file,""); zos.close(); } public void compress(java.util.zip.ZipOutputStream zos,java.io.File file,String context
    )throws java.io.IOException { if(file.isFile()) { byte by[] = new byte[(int) file.length()]; java.io.FileInputStream is = new java.io.FileInputStream(file); is.read(by); is.close(); java.util.zip.ZipEntry ze=new java.util.zip.ZipEntry(context+file.getName()); zos.putNextEntry(ze); zos.write(by); zos.closeEntry(); }else if(file.isDirectory()) { java.io.File files[]=file.listFiles(); if(files!=null) for(int index=0;index<files.length;index++) { compress(zos,files[index],context+"/"+file.getName()+"/"); } } }
    相关文章
    本页查看次数: