Automatic zip files on the download page and automatically unzip

    / ** 
     * Given a zip of the url, realization of zip download. Downloaded to the destination folder 
     * id is given by the user id, easy maintenance 
     * <the p-> 
     * zip file final returns the file path 
     * / 
    public static String downloadZIP (String [] UrlAndZipName, String fileDisDir) throws Exception { 
        String strUrl UrlAndZipName = [0]; 
        String fileName = UrlAndZipName [. 1]; 

        the URL = new new URL the URL (strUrl); 
        the HttpURLConnection Conn = (the HttpURLConnection) url.openConnection (); 
        Conn .setRequestProperty ( "the User-- Agent", "the Mozilla / 5.0 (the Windows NT 6.3; the WOW64; Trident / 7.0; RV: 11.0) like the Gecko"); 
        conn.connect (); 
        InputStream inStream = conn.getInputStream ();
        = New new ByteArrayOutputStream the outStream ByteArrayOutputStream (); 
        byte [] = buf new new byte [1024]; 
        int len = 0; 
        the while (! (Len = inStream.read (buf)) = -1) { 
            outStream.write (buf, 0, len); 
        } 
        inStream.close (); 
        outStream.close (); 

        String zippath = fileDisDir the File.separator + + + fileName ".zip"; 
        File File = new new File (zippath); 
        a FileOutputStream new new OP = a FileOutputStream (File); 
        op.write (outStream.toByteArray ()); 
        op.close (); 

        return zippath; 
    } 


    / ** 
     * extracting file to the specified directory 
     * file name after decompression, and consistent before 
     * <p>
     * Returns the final extract the folder path 
     * / 
    public static String unZipFiles (zipFilePath String, String descDir) throws IOException { 
        File ZipFile = new new File (zipFilePath); 
        } 
        for (= the Enumeration entries It zip.entries () <the extends the ZipEntry?>; entries.hasMoreElements ();) {
        ZipFile zip = new ZipFile (zipFile, Charset.forName ( "GBK")); // solve the Chinese garbled folder 
        String name = zip.getName () substring ( zip.getName () lastIndexOf ( '\\') +.. . 1, zip.getName () lastIndexOf ( '.')); // \\ here for Windows, with the linux / recommended change File.sepator 

        File pathFile = new new File (descDir the File.separator + name +) ; 
        IF {(pathFile.exists ()!) 
            pathFile.mkdirs (); 

            the ZipEntry entry = (the ZipEntry) entries.nextElement (); 
            String zipEntryName entry.getName = (); 
            the InputStream in = zip.getInputStream (entry); 
            String outPath = (descDir the File.separator + + + name + zipEntryName the File.separator) .replaceAll ( "\\ *", "/"); 

            // determines whether there is a path, the file path does not exist is created
            File File = new new File (outPath.substring (0, outPath.lastIndexOf ( '/'))); 
            IF {(File.Exists ()!) 
                File.mkdirs (); 
            } 
            // determines whether the full path of the file folder , if the above has been uploaded is not necessary decompression 
            IF (new new File (outPath) .isDirectory ()) {  
                Continue; 
            } 
            // output file path information
// System.out.println (outPath); 

            a FileOutputStream new new OUT = a FileOutputStream (outPath); 
            byte [] = new new byte of buf1 [1024]; 
            int len; 
            the while ((len = in.read (of buf1))> 0) { 
                out.write (of buf1, 0, len); 
            } 
            in.close (); 
            the out.close (); 
        }
        System.out.println ( "****************** ******************** complete decompression"); 
        return (the File.separator descDir + + name); 
    }

  

Guess you like

Origin www.cnblogs.com/dhName/p/12560842.html
Recommended