JAVA遠程下載zip包並解壓到指定目錄

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Test {
public static void main(String[] args) {
String url = "http://192.168.20.24:8083/luaFile/lua.zip";
DownAndReadFile(url);
}

/**
   * 遠程下載文件並讀取返回p
   * @param filePath 文件網絡地址 如http://www.baidu.com/1.txt
   * @return String
   */
  public static void DownAndReadFile(String filePath){
  String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());  
  String dir = "D://" + date;
  File savePath = new File(dir);//創建新文件
  if (!savePath.exists()) { 
  savePath.mkdir(); 
      }
  String[] urlname = filePath.split("/");
  int len = urlname.length-1;
  String uname = urlname[len];//獲取文件名
  try {
  File file = new File(savePath+"//"+uname);//創建新文件
  if(file!=null && !file.exists()){
  file.createNewFile();
  }
  OutputStream oputstream = new FileOutputStream(file);
  URL url = new URL(filePath);
  HttpURLConnection uc = (HttpURLConnection) url.openConnection();
  uc.setDoInput(true);//設置是否要從 URL 連接讀取數據,默認爲true
  uc.connect();
  InputStream iputstream = uc.getInputStream();
  System.out.println("file size is:"+uc.getContentLength());//打印文件長度
  byte[] buffer = new byte[4*1024];
  int byteRead = -1;
  while((byteRead=(iputstream.read(buffer)))!= -1){
  oputstream.write(buffer, 0, byteRead);
  }
  oputstream.flush();
  iputstream.close();
  oputstream.close();
  //讀取文件
  StringBuffer strb = new StringBuffer();
  FileInputStream fs = new FileInputStream(new File(savePath+"//"+uname));
  InputStreamReader isr = new InputStreamReader(fs,"UTF-8");
  BufferedReader br = new BufferedReader(isr);
  String data = "";
  while((data = br.readLine()) != null){
  strb.append(data + "\n");
  }
  br.close();
  fs.close();
  isr.close();
  //解壓
  unZipFiles(dir + "/lua.zip", dir + "/");
} catch (Exception e) {
System.out.println("讀取失敗!");
e.printStackTrace();
}   
  }
  
  /**
* 解壓到指定目錄
* @param zipPath
* @param descDir
* @author isea533
*/
public static void unZipFiles(String zipPath,String descDir)throws IOException{
unZipFiles(new File(zipPath), descDir);
}
/**
* 解壓文件到指定目錄
* @param zipFile
* @param descDir
* @author isea533
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile,String descDir)throws IOException{
File pathFile = new File(descDir);
if(!pathFile.exists()){
pathFile.mkdirs();
}
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries = zip.entries();entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
String outPath = (descDir+zipEntryName).replaceAll("\\*", "/");;
//判斷路徑是否存在,不存在則創建文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判斷文件全路徑是否爲文件夾,如果是上面已經上傳,不需要解壓
if(new File(outPath).isDirectory()){
continue;
}
//輸出文件路徑信息
System.out.println(outPath);

OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
System.out.println("******************解壓完畢********************");
}

}



=========================================

運行結果:

file size is:8317
D://2014-09-09/math_basic.lua
D://2014-09-09/pet.lua
D://2014-09-09/pet_explore_reward.lua
D://2014-09-09/pet_explore_reward_subsidize.lua
D://2014-09-09/point.lua
D://2014-09-09/pvp.lua
D://2014-09-09/_res.lst
D://2014-09-09/altaricon.lua
D://2014-09-09/daily_quest.lua
D://2014-09-09/fight.lua
D://2014-09-09/gameDesign.lua
D://2014-09-09/goldcost.lua
D://2014-09-09/goldoutput.lua
******************解壓完畢********************

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章