您的位置:首页 > 编程语言 > Java开发

java对tar、zip包的相关操作

2016-04-19 11:42 288 查看
package com.mash5.web.services.util;

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

import org.bson.types.ObjectId;

import com.ice.tar.TarEntry;

import com.ice.tar.TarInputStream;

import com.mongodb.DB;

import com.mongodb.Mongo;

import com.mongodb.gridfs.GridFS;

import com.mongodb.gridfs.GridFSDBFile;

import com.mongodb.gridfs.GridFSInputFile;

public class ParseTar_Zip_Jar {

/**
* 解压tar包
* @param tarPath
* @param basePath
* @return
*/
public static File getFileListByTarPath(String tarPath, String basePath) {
if(tarPath == null) return null;
try {
OutputStream out =  null;
TarEntry entry = null;
TarInputStream tar = new TarInputStream(new FileInputStream(new File(tarPath)));
File returnFile = null;
int i = 0;
while((entry = tar.getNextEntry()) != null) {
if(i == 0) {
File outFile = new File(basePath + entry.getName());
outFile.mkdir();
returnFile = outFile;
}
i ++;
if(entry.isDirectory()) {
continue;
}
File outFile = new File(basePath + entry.getName());
new File(outFile.getParent()).mkdirs();
out = new FileOutputStream(outFile);
int data = -1;
while((data = tar.read()) != -1) {
out.write(data);
}
out.close();
}
tar.close();
return returnFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 解压Zip包
* @param zipPath
* @param basePath
* @return
*/
public static File getFileListByZipPath(String zipPath, String basePath) {
if(zipPath == null || "".equals(zipPath)) return null;
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(new File(zipPath)));
ZipEntry entry = null;
OutputStream out = null;
File returnFile = null;
int i = 0;
while((entry = in.getNextEntry()) != null) {
if(i == 0) {
File outFile = new File(basePath + entry.getName());
returnFile = outFile;
}
i ++;
if(entry.isDirectory()) {
continue;
}
File outFile = new File(basePath + entry.getName());
//创建他的父文件夹
new File(outFile.getParent()).mkdirs();
out = new FileOutputStream(outFile);
int data = -1;
while((data = in.read()) != -1) {
out.write(data);
}
out.close();
}
in.close();
return returnFile;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* 解压Zip包
* @param zipPath
* @param basePath
* @return
*/
public static List<GridFSDBFile> getFilesByFSZip(GridFSDBFile file, GridFS myFS) {
if(file == null || "".equals(file)) return null;
List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();

try {
ZipInputStream zis = new ZipInputStream(file.getInputStream());
ZipEntry entry = null;
while(( entry = zis.getNextEntry()) != null) {
if(entry.isDirectory()) continue;
GridFSInputFile inputFile  = myFS.createFile(zis);
inputFile.setFilename(entry.getName());
inputFile.setContentType(file.getMD5());
inputFile.save();
System.out.println(entry.getName()+","+inputFile.getId().toString());
ObjectId fileId = new ObjectId(inputFile.getId().toString());
GridFSDBFile fileObj = myFS.find(fileId);
files.add(fileObj);
}

zis.close();
return files;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static void zipFS(List<GridFSDBFile> fileList,String zipFileName) throws Exception{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
for(GridFSDBFile file : fileList){
zipFS(zos, file);
}
zos.close();
}

/**
* @param zipFileName
*            生成的压缩文件夹路径
* @param filePath
*            源文件夹
* @throws FileNotFoundException
* @throws IOException
*/

public static void zip(String zipFileName, List<String> filePath) throws FileNotFoundException, IOException {
ZipOutputStream zos = null;
zos = new ZipOutputStream(new FileOutputStream(zipFileName));
boolean has=false;
for(String str : filePath) {
File f=new File(str);
if(f.exists()){
has=true;
zip(zos, f, "");
}
}
if(!has)zos.putNextEntry(new ZipEntry(""));
zos.close();
}

public static void zip(String zipFileName){
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(new FileOutputStream(zipFileName));
zos.putNextEntry(new ZipEntry(""));
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void zipFS (ZipOutputStream zos, GridFSDBFile file) throws Exception {
InputStream is = file.getInputStream();
BufferedInputStream bis =  new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(file.getFilename()));
int length = 0;
byte[] buffer = new byte[1024 * 1024];
while ((length = bis.read(buffer)) != -1) {
zos.write(buffer, 0, length);
zos.flush();
}
is.close();
bis.close();
}

/**
*
* @param zos
* @param f
*            源文件夹
* @param base
* @throws FileNotFoundException
* @throws IOException
*/
private static void zip(ZipOutputStream zos, File f, String base) throws FileNotFoundException, IOException {
BufferedInputStream bis = null;
FileInputStream fis = null;
if (f.isDirectory()) {
File[] files = f.listFiles();
if(base.equals("")){
base=f.getName()+"/";
} else {
base=base+"/";
}
zos.putNextEntry(new ZipEntry(base));
for (int i = 0; i < files.length; i++) {
zip(zos, files[i], base + files[i].getName());
}
} else {
if(base.equals("")){
base=f.getName();
}
zos.putNextEntry(new ZipEntry(base));
fis = new FileInputStream(f);
bis = new BufferedInputStream(fis);
byte[] b = new byte[1024 * 1024];
int len = 0;
while ((len = bis.read(b)) != -1) {
zos.write(b, 0, len);
zos.flush();
}
fis.close();
bis.close();
}
}

public static void main(String[] args) throws Exception {
Mongo db = new Mongo("192.168.3.171", 30000);
DB mydb = db.getDB("mash5");

GridFS myFS = new GridFS(mydb);

//打包文件
/*
List<GridFSDBFile> fileList = new ArrayList<GridFSDBFile>();

ObjectId fileId = new ObjectId("54bf1389fffe27eced2f5e7f");
GridFSDBFile file = myFS.find(fileId);
fileList.add(file);

ObjectId fileId1 = new ObjectId("54bdbe2ba43e5ada8d875439");
GridFSDBFile file1 = myFS.find(fileId1);
fileList.add(file1);

ObjectId fileId2 = new ObjectId("54bdb6b1a43ee44e955eec45");
GridFSDBFile file2 = myFS.find(fileId2);
fileList.add(file2);

zipFS(fileList, "D:/0203-zh.rar");
*/

//解压包

ObjectId fileId = new ObjectId("54d1dd6bfe1cae7da80f6b68");
GridFSDBFile file = myFS.find(fileId);
List<GridFSDBFile> fileList = ParseTar_Zip_Jar.getFilesByFSZip(file, myFS);
for(GridFSDBFile fileObj : fileList){
InputStream is = fileObj.getInputStream();
String fileName = fileObj.getFilename();
int lastIndex = fileName.lastIndexOf("/");
fileName = fileName.substring(lastIndex+1, fileName.length());
FileOutputStream out = new FileOutputStream(new File("D:/test/"+fileName));
//设置缓存;
int length = 0;
byte[] buffer = new byte[1024000];
while ((length = is.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
// 关闭对应的流
out.flush();
is.close();
out.close();

}

//解压tar包成功


// List fileList = ParseTar_Zip_Jar.getFileListByTarPath(“e:/WEB-INF.tar”, “e:/a/”);

// for(File f : fileList) {

// System.out.println(f.getAbsolutePath());

// }

//解压zip包成功
//ParseTar_Zip_Jar.getFileListByZipPath("f:/my.zip", "f:/my/");


// for(File f : fileList) {

// System.out.println(f.getAbsolutePath());

// }

// File f = new File(“e:/WEB-INF.tar”);

// System.out.println(f.length());

// List filePathList = new ArrayList();

// filePathList.add(“F:\a”);

// filePathList.add(“F:\b”);

// ParseTar_Zip_Jar.getTarByFileList(filePathList);

// System.out.println(new Date().toLocaleString());

//List<String> filePathList = new ArrayList<String>();


// filePathList.add(“f:/a/a/a.txt”);

// filePathList.add(“f:/c.txt”);

// try {

// ParseTar_Zip_Jar.zip(“F:/my.zip”, filePathList);

// } catch (FileNotFoundException e) {

// e.printStackTrace();

// } catch (IOException e) {

// e.printStackTrace();

// }

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: