java实现HDFS上的压缩文件的解压

来源:互联网 发布:麦肯锡7s模型知乎 编辑:程序博客网 时间:2024/06/10 07:51

最近在学习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();
}
}
}


0 0