别人写的加压

由于频繁需要解压ZIP,加压ZIP


FileItem:


Java代码
public class FileItem {
private String fileName;
private byte[] fileContent;

public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

public byte[] getFileContent() {
return fileContent;
}

public void setFileContent(byte[] fileContent) {
this.fileContent = fileContent;
}
}


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class FileUtil {
/**缓存大小*/
private final static int BUFFER_SIZE = 8192;
/**后缀名*/
private final static String SUFFIX = "zip";
/**后缀名分隔符*/
private final static String POINT = ".";

// /**
// 26. * 获得字节数组
// 27. *
// 28. * @param filepath 文件全路径
// 29. * @return 字节数组
// 30. * @throws IOException IOException
// 31. */

public static byte[] getBytes(File file) throws IOException{
return getBytes(file.getAbsolutePath());
}
public static byte[] getBytes(String filepath) throws IOException {
BufferedInputStream bis = null;
ByteArrayOutputStream byteArrayOutputStream = null;
BufferedOutputStream bos = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(byteArrayOutputStream);
byte[] bytes = new byte[BUFFER_SIZE];
int len;
while ((len = bis.read(bytes, 0, BUFFER_SIZE)) != -1) {
bos.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw e;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != byteArrayOutputStream) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return byteArrayOutputStream.toByteArray();
}

// /**
// 82. * 统一文件分隔符为LINUX,类UNIX文件系统风格
// 83. *
// 84. * @param direcotory 目录
// 85. * @return 统一处理后目录字符串风格
// 86. */
private static String replaceAllSeparator(String direcotory) {
String str = direcotory.replaceAll("\\\\", "/");
return str.length() != str.lastIndexOf("/") ? str + "/" : str;
}

// 92. /**
// 93. * 将文件项写到文件
// 94. *
// 95. * @param fileItem 文件项
// 96. * @param directory 目录
// 97. * @throws IOException IOException
// 98. */
public static void writeFile(FileItem fileItem, String directory)
throws IOException {
File dir = new File(directory);
if (!dir.exists()) {
dir.mkdirs();
}
writeFile(fileItem.getFileContent(), replaceAllSeparator(directory)
+ fileItem.getFileName());
}

// 109. /**
// 110. * 写文件
// 111. *
// 112. * @param fileContent 文件二进制流
// 113. * @param filepath 文件全路径
// 114. * @throws IOException IOException
// 115. */
public static void writeFile(byte[] fileContent, String filepath)
throws IOException {
InputStream inputStream = new ByteArrayInputStream(fileContent);
writeFile(inputStream, filepath);
}

// 122. /**
// 123. * 将流写到一个文件
// 124. *
// 125. * @param inputStream 文件流
// 126. * @param filepath 文件路径
// 127. * @throws IOException IOException
// 128. */
public static void writeFile(InputStream inputStream, String filepath)
throws IOException {
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
File file = new File(filepath);
if (!file.exists()) {
file.createNewFile();
}
bos = new BufferedOutputStream(new FileOutputStream(file));
bis = new BufferedInputStream(inputStream);
byte[] buffer = new byte[BUFFER_SIZE];
int len = -1;
while ((len = bis.read(buffer, 0, BUFFER_SIZE)) != -1) {
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (null != bos)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (null != bis)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

// 164. /**
// 165. * 解压zip文件
// 166. * @param filepath 文件全路径
// 167. * @return 解压后的文件项
// 168. * @throws IOException IOException
// 169. */
public static List uncompressZip(String filepath) throws IOException {
ByteArrayOutputStream baos = null;
BufferedOutputStream bos = null;
ZipInputStream zis = null;
// zipInputStream
List fileList = null;
try {
zis = new ZipInputStream(new FileInputStream(filepath));
baos = new ByteArrayOutputStream();
bos = new BufferedOutputStream(baos);
fileList = new ArrayList();
byte[] buffer = new byte[BUFFER_SIZE];
int len = -1;
ZipEntry zipEntry = null;
while (null != (zipEntry = zis.getNextEntry())) {
if (null != zipEntry) {
System.out.println("Name:" + zipEntry.getName() + ",Size:"
+ zipEntry.getSize() + "bytes");
while (-1 != (len = zis.read(buffer))) {
bos.write(buffer, 0, len);
}
bos.flush();
byte[] fileContent = baos.toByteArray();
FileItem item = new FileItem();
item.setFileName(zipEntry.getName());
item.setFileContent(fileContent);
fileList.add(item);
}
}
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (null != zis) {
try {
zis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != baos) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileList;
}

// 228. /**
// 229. * 将目录压缩成zip包
// 230. *
// 231. * @param filepath
// 232. * 文件全路径
// 233. * @throws IOException
// 234. * IOException
// 235. */
public static FileItem compressZip(String filepath) throws IOException {
File directory = new File(filepath);
String filename = directory.getName();
File[] fileList = directory.listFiles();
ZipOutputStream zos = null;
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
FileItem fileItem = null;
try {
baos = new ByteArrayOutputStream();
zos = new ZipOutputStream(baos);
byte[] buffer = new byte[BUFFER_SIZE];
int len = -1;
for (File file : fileList) {
bis = new BufferedInputStream(new FileInputStream(file));
while (-1 != (len = bis.read(buffer, 0, BUFFER_SIZE))) {
//ZipEntry为Zip压缩文件的每一项
ZipEntry zipEntry = new ZipEntry(file.getName());
zipEntry.setSize(file.length());
zipEntry.setCompressedSize(file.length());
zos.putNextEntry(zipEntry);
zos.write(buffer, 0, len);
}
//做完一个文件的读写应清下缓存
zos.flush();
if (null != bis) {
bis.close();
}
}
//完成所有文件的读取,转化为一个二进制流的包装zos
zos.finish();
fileItem = new FileItem();
int index = filename.lastIndexOf(".");
filename = filename.substring(0, -1 == index ? filename.length()
: index);
//要生成zip文件的文件名
fileItem.setFileName(filename + POINT + SUFFIX);
//要生成的zip文件的二进制流
fileItem.setFileContent(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (null != baos) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != zos) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileItem;
}

/**
* 解压zip文件
*
* @param zipFile zip文件
* @return 解压后的文件项
* @throws IOException IOException
*/
public static List uncompressZip(File zipFile) throws IOException {
String filepath = zipFile.getAbsolutePath();
return uncompressZip(filepath);
}

public static void main(String[] args) throws IOException {
//加压
FileItem fileItem = compressZip("F:/aa");
//将SCYSB_1234567890_0901_20100912082719250.zip文件写到F:/zjie目录
FileUtil.writeFile(fileItem, "F:/zjie");
//解压F:/zjie/SCYSB_1234567890_0901_20100912082719250.zip文件
List fileItemList = FileUtil
.uncompressZip("F:/zjie/aa.zip");
//将解压后的文件流生成文件写到F:/zjie/SCYSB_1234567890_0901_20100912082719250/目录
for (int i = 0; i < fileItemList.size(); i++) {
FileItem item = (FileItem) fileItemList.get(i);
// DbfUtil.dbfReader(item.getFileContent());
FileUtil.writeFile(item.getFileContent(), "F:/zjie/SCYSB_1234567890_0901_20100912082719250/"+item.getFileName());
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值