自動ジップダウンロードページ上のファイルと自動的に解凍

    / ** 
     * URLのジップ、ジップダウンロードの実現を考える。ダウンロード先のフォルダに
     * idは、ユーザID、メンテナンスが容易で与えられる
     * <p型> 
     * zipファイルの最終リターンファイルパス
     * / 
    パブリック静的文字列downloadZIP (文字列[] UrlAndZipName、ストリングfileDisDir)は例外{スロー
        列strUrl UrlAndZipName = [0]; 
        文字列fileNameに= UrlAndZipName [1]; 

        URL =新しい新しいURL URL(strUrl); 
        HttpURLConnectionのコン=(HttpURLConnectionの)url.openConnection(); 
        コネティカット.setRequestProperty( "User--エージェント"、 "Mozillaの/ 5.0(Windows NTの6.3; WOW64;トライデント/ 7.0; RV:11.0)Geckoのような"); 
        conn.connect(); 
        、InputStreamであるinStream = conn.getInputStream();
        =新規新しいByteArrayOutputStream OUTSTREAM ByteArrayOutputStream(); 
        バイト[] =新しい新しいバイト[1024] BUF; 
     *指定されたディレクトリにファイルを解凍
     する前に、解凍後の*ファイル名、および一貫した
     * <P>
        int型のlen = 0; 
        (!(LEN = inStream.read(BUF))= -1){一方
            outStream.write(BUF、0、LEN)。
        } 
        inStream.close()。
        outStream.close(); 

        文字列zippath = fileDisDir + File.separator + fileNameに+ ".zipファイル"; 
        ファイルファイル=新しいファイル(zippath)。
        FileOutputStreamのOP =新たFileOutputStream(ファイル); 
        op.write(outStream.toByteArray())。
        op.close(); 

        zippath返します。
    } 


    / ** 
     *返回最终解压的文件夹路径
     * / 
    パブリック静的な文字列unZipFiles(文字列zipFilePath、列descDirは)のIOException {スロー
        ファイルzipファイル=新しいファイル(zipFilePath)を、
        ZIPファイルは、ZIP =新しいZIPファイル(zipファイル、 Charset.forName( "GBKは")); // 中国を解決するためのフォルダが文字化け
        文字列名= zip.getName()サブストリング( + ...( '\\')zip.getName()のlastIndexOf 。1、zip.getName()のlastIndexOf( '')); // \\ ここでは、Windows用、Linuxの/推奨変更File.sepatorを持つ

        pathFile =新しい新しいファイル(descDir File.separator +名+ファイル) ; 
        IF {(pathFile.exists()!)
            pathFile.mkdirs();  
        } 

        (これはzip.entries =列挙エントリ()<またはZipEntry延び?>; entries.hasMoreElements();)のために{
            またはZipEntryエントリ=(またはZipEntry)entries.nextElement(); 
            文字列zipEntryName entry.getName =(); 
            = zip.getInputStreamにおける入力ストリーム(エントリー); 
            文字列OUTPATH =(descDir File.separator + + +名+ zipEntryName File.separator).replaceAll( "\\ *"、 "/"); 

            //パスがあるか否かを判定し、ファイルのパスが作成されますが存在しません
            ファイルファイル=新しい新しいファイル(outPath.substring(0、outPath.lastIndexOf( '/'))); 
            IF {(File.Exists()!)
                File.mkdirs(); 
            } 
            //ファイルフォルダのフルパスかどうかを決定します、上記アップロードされた場合には、必要解凍ない
            (新しい新規ファイル(OUTPATH).isDirectory()){IF 
                続行; 
            } 
            //出力ファイルパス情報
//するSystem.out.println(OUTPATH)。

            FileOutputStreamアウト=新しいのFileOutputStream(OUTPATH)。
            バイト[] BUF1 =新しいバイト[1024]; 
            lenはint型。
            一方、((LEN = in.read(BUF1))> 0){ 
                out.write(BUF1、0、LEN)。
            } 
            in.close()。
            out.close(); 
        }
        System.out.println( "****************** ********************完全に解凍"); 
        リターン(File.separator descDir + +名); 
    }

  

おすすめ

転載: www.cnblogs.com/dhName/p/12560842.html
おすすめ