1. 程式人生 > >java實現HDFS上的壓縮檔案的解壓

java實現HDFS上的壓縮檔案的解壓

最近在學習hadoop的影象處理,但是鑑於影象都是小檔案,而hadoop適合大檔案,所以將影象壓縮上傳到HDFS。現在通過Java程式設計將上傳到HDFS的檔案解壓。附上程式碼:

package accurad.dcx.du;
//實現下載與解壓
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DownUnzip {




/**
*  @author dcx by 2015.11.19
* 檔案下載
* @param src
* @param dst
* @param conf
* @return
*/
public static boolean getFromHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = dstPath.getFileSystem(conf) ;
dhfs.copyToLocalFile(false, new Path(src), dstPath) ;
}catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}

/**
* 解壓縮zip包
* @param zipFilePath zip檔案的全路徑
* @param unzipFilePath 解壓後的檔案儲存的路徑
* @param includeZipFileName 解壓後的檔案儲存的路徑是否包含壓縮檔案的檔名。true-包含;false-不包含
*/
@SuppressWarnings("unchecked")
public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception
{
if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(unzipFilePath))
{
//throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
File zipFile = new File(zipFilePath);
//如果解壓後的檔案儲存路徑包含壓縮檔案的檔名,則追加該檔名到解壓路徑
if (includeZipFileName)
{
String fileName = zipFile.getName();
if (StringUtils.isNotEmpty(fileName))
{
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
unzipFilePath = unzipFilePath + File.separator + fileName;
}
//建立解壓縮檔案儲存的路徑
File unzipFileDir = new File(unzipFilePath);
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())
{
unzipFileDir.mkdirs();
}

//開始解壓
ZipEntry entry = null;
String entryFilePath = null, entryDirPath = null;
File entryFile = null, entryDir = null;
int index = 0, count = 0, bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
ZipFile zip = new ZipFile(zipFile);
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();
//迴圈對壓縮包裡的每一個檔案進行解壓
while(entries.hasMoreElements())
{
entry = entries.nextElement();
//構建壓縮包中一個檔案解壓後儲存的檔案全路徑
entryFilePath = unzipFilePath + File.separator + entry.getName();
//構建解壓後儲存的資料夾路徑
index = entryFilePath.lastIndexOf(File.separator);
if (index != -1)
{
entryDirPath = entryFilePath.substring(0, index);
}
else
{
entryDirPath = "";
}
entryDir = new File(entryDirPath);
//如果資料夾路徑不存在,則建立資料夾
if (!entryDir.exists() || !entryDir.isDirectory())
{
entryDir.mkdirs();
}

//建立解壓檔案
entryFile = new File(entryFilePath);
if (entryFile.exists())
{
//檢測檔案是否允許刪除,如果不允許刪除,將會丟擲SecurityException
SecurityManager securityManager = new SecurityManager();
securityManager.checkDelete(entryFilePath);
//刪除已存在的目標檔案
entryFile.delete();
}

//寫入檔案
bos = new BufferedOutputStream(new FileOutputStream(entryFile));
bis = new BufferedInputStream(zip.getInputStream(entry));
while ((count = bis.read(buffer, 0, bufferSize)) != -1)
{
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
}
    
public static void main(String[] args) throws IOException {  
boolean status = false ;
Configuration myConf = new Configuration();
myConf.set("mapred.job.tracker", "192.168.1.224:9001");  //改成自己的IP
//設定你的NameNode地址
myConf.set("fs.default.name", "hdfs://192.168.1.224:9000");
String dst = "I:\\face_detect\\";
//HDFS的路徑
String src = "hdfs://192.168.1.224:9000/user/face_bg.zip" ;
//net上傳
try
{
//下載
status = getFromHDFS( src ,  dst ,  myConf) ;
System.out.println("status="+status) ;
   //解壓
   unzip( dst+"\\face_bg.zip",dst,status);
   //預處理 資料編譯 與訓練 及結果上傳 
   Runtime.getRuntime().exec("I:\\face_detect\\pre_dscompile.bat"); 
   

catch (Exception e)
{
e.printStackTrace();
}
}
}