java读取压缩文件并压缩
import java.io.BufferedInputStream;
创新互联专业为企业提供滕州网站建设、滕州做网站、滕州网站设计、滕州网站制作等企业网站建设、网页设计与制作、滕州企业网站模板建站服务,10年滕州做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
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 JZip {
public static int iCompressLevel;//压缩比 取值范围为0~9
public static boolean bOverWrite;//是否覆盖同名文件 取值范围为True和False
@SuppressWarnings("unchecked")
private static ArrayList AllFiles = new ArrayList();
public static String sErrorMessage;
private String zipFilePath;
public ListFile srcMap;
public JZip () {
iCompressLevel = 9;
// bOverWrite=true;
}
public JZip(String zipFilePath) throws FileNotFoundException, IOException {
this.zipFilePath = zipFilePath;
}
@SuppressWarnings("unchecked")
public static ArrayList extract (String sZipPathFile , String sDestPath) {
ArrayList AllFileName = new ArrayList();
try {
//先指定压缩档的位置和档名,建立FileInputStream对象
FileInputStream fins = new FileInputStream(sZipPathFile);
//将fins传入ZipInputStream中
ZipInputStream zins = new ZipInputStream(fins);
ZipEntry ze = null;
byte ch[] = new byte[256];
while ((ze = zins.getNextEntry()) != null) {
File zfile = new File(sDestPath + ze.getName());
File fpath = new File(zfile.getParentFile().getPath());
if (ze.isDirectory()) {
if (!zfile.exists())
zfile.mkdirs();
zins.closeEntry();
} else {
if (!fpath.exists())
fpath.mkdirs();
FileOutputStream fouts = new FileOutputStream(zfile);
int i;
AllFileName.add(zfile.getAbsolutePath());
while ((i = zins.read(ch)) != -1)
fouts.write(ch, 0, i);
zins.closeEntry();
fouts.close();
}
}
fins.close();
zins.close();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Extract error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
AllFiles.clear();
return AllFileName;
}
@SuppressWarnings({ "unchecked", "static-access" })
public static void compress (String sPathFile , boolean bIsPath , String sZipPathFile) {
try {
String sPath;
//先指定压缩档的位置及档名,建立一个FileOutputStream
FileOutputStream fos = new FileOutputStream(sZipPathFile);
//建立ZipOutputStream并将fos传入
ZipOutputStream zos = new ZipOutputStream(fos);
//设置压缩比
zos.setLevel(iCompressLevel);
if (bIsPath == true) {
searchFiles(sPathFile);
sPath = sPathFile;
} else {
File myfile = new File(sPathFile);
sPath = sPathFile.substring(0, sPathFile.lastIndexOf(myfile.separator) + 1);
AllFiles.add(myfile);
}
Object[] myobject = AllFiles.toArray();
ZipEntry ze = null;
//每个档案要压缩,都要透过ZipEntry来处理
FileInputStream fis = null;
BufferedReader in = null;
//byte[] ch = new byte[256];
for (int i = 0 ; i myobject.length ; i++) {
File myfile = (File) myobject[i];
if (myfile.isFile()) {
in = new BufferedReader(new InputStreamReader(new FileInputStream(myfile.getPath()),"iso8859-1"));
//以档案的名字当Entry,也可以自己再加上额外的路径
//例如 ze=new ZipEntry("test\\"+myfiles[i].getName());
//如此压缩档内的每个档案都会加test这个路径
ze = new ZipEntry(myfile.getPath().substring((sPath).length()));
//将ZipEntry透过ZipOutputStream的putNextEntry的方式送进去处理
fis = new FileInputStream(myfile);
zos.putNextEntry(ze);
int len = 0;
//开始将原始档案读进ZipOutputStream
while ((len = in.read()) != -1) {
zos.write(len);
}
fis.close();
zos.closeEntry();
}
}
zos.close();
fos.close();
AllFiles.clear();
sErrorMessage = "OK";
} catch (Exception e) {
System.err.println("Compress error:" + e.getMessage());
sErrorMessage = e.getMessage();
}
}
/*
这是一个递归过程,功能是检索出所有的文件名称
dirstr:目录名称
*/
@SuppressWarnings("unchecked")
private static void searchFiles (String dirstr) {
File tempdir = new File(dirstr);
if (tempdir.exists()) {
if (tempdir.isDirectory()) {
File[] tempfiles = tempdir.listFiles();
for (int i = 0 ; i tempfiles.length ; i++) {
if (tempfiles[i].isDirectory())
searchFiles(tempfiles[i].getPath());
else {
AllFiles.add(tempfiles[i]);
}
}
} else {
AllFiles.add(tempdir);
}
}
}
public String getZipFilePath() {
return zipFilePath;
}
public void setZipFilePath(String zipFilePath) {
this.zipFilePath = zipFilePath;
}
/**
* 解析zip文件得到文件名
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean parserZip() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream(zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
try {
srcMap = new ArrayListFile();
while ((entry = zis.getNextEntry()) != null) {
File file = new File(zipFilePath + File.separator + entry.getName());
srcMap.add(file);
}
zis.close();
fis.close();
return true;
} catch (IOException e) {
return false;
}
}
/**
*
* @param zipFileName 待解压缩的ZIP文件
* @param extPlace 解压后的文件夹
*/
public static void extZipFileList(String zipFileName, String extPlace) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName));
File files = new File(extPlace);
files.mkdirs();
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
String entryName = entry.getName();
if (entry.isDirectory()) {
File file = new File(files + entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
} else {
OutputStream os = new FileOutputStream(files+File.separator + entryName);
// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry();
System.out.println("解压文件:" + entryName);
}
}
} catch (IOException e) {
}
}
@SuppressWarnings("static-access")
public static void main(String args[]){
}
}
怎样用java快速实现zip文件的压缩解压缩
package zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import org.apache.commons.lang3.StringUtils;
public class ZipUtil {
/**
* 递归压缩文件夹
* @param srcRootDir 压缩文件夹根目录的子路径
* @param file 当前递归压缩的文件或目录对象
* @param zos 压缩文件存储对象
* @throws Exception
*/
private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception
{
if (file == null)
{
return;
}
//如果是文件,则直接压缩该文件
if (file.isFile())
{
int count, bufferLen = 1024;
byte data[] = new byte[bufferLen];
//获取文件相对于压缩文件夹根目录的子路径
String subPath = file.getAbsolutePath();
int index = subPath.indexOf(srcRootDir);
if (index != -1)
{
subPath = subPath.substring(srcRootDir.length() + File.separator.length());
}
ZipEntry entry = new ZipEntry(subPath);
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
while ((count = bis.read(data, 0, bufferLen)) != -1)
{
zos.write(data, 0, count);
}
bis.close();
zos.closeEntry();
}
//如果是目录,则压缩整个目录
else
{
//压缩目录中的文件或子目录
File[] childFileList = file.listFiles();
for (int n=0; nchildFileList.length; n++)
{
childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());
zip(srcRootDir, childFileList[n], zos);
}
}
}
/**
* 对文件或文件目录进行压缩
* @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径
* @param zipPath 压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹
* @param zipFileName 压缩文件名
* @throws Exception
*/
public static void zip(String srcPath, String zipPath, String zipFileName) throws Exception
{
if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(zipPath) || StringUtils.isEmpty(zipFileName))
{
throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);
}
CheckedOutputStream cos = null;
ZipOutputStream zos = null;
try
{
File srcFile = new File(srcPath);
//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)
if (srcFile.isDirectory() zipPath.indexOf(srcPath)!=-1)
{
throw new ParameterException(ICommonResultCode.INVALID_PARAMETER, "zipPath must not be the child directory of srcPath.");
}
//判断压缩文件保存的路径是否存在,如果不存在,则创建目录
File zipDir = new File(zipPath);
if (!zipDir.exists() || !zipDir.isDirectory())
{
zipDir.mkdirs();
}
//创建压缩文件保存的文件对象
String zipFilePath = zipPath + File.separator + zipFileName;
File zipFile = new File(zipFilePath);
if (zipFile.exists())
{
//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException
SecurityManager securityManager = new SecurityManager();
securityManager.checkDelete(zipFilePath);
//删除已存在的目标文件
zipFile.delete();
}
cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());
zos = new ZipOutputStream(cos);
//如果只是压缩一个文件,则需要截取该文件的父目录
String srcRootDir = srcPath;
if (srcFile.isFile())
{
int index = srcPath.lastIndexOf(File.separator);
if (index != -1)
{
srcRootDir = srcPath.substring(0, index);
}
}
//调用递归压缩方法进行目录或文件压缩
zip(srcRootDir, srcFile, zos);
zos.flush();
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if (zos != null)
{
zos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 解压缩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);
EnumerationZipEntry entries = (EnumerationZipEntry)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)
{
String zipPath = "d:\\ziptest\\zipPath";
String dir = "d:\\ziptest\\rawfiles";
String zipFileName = "test.zip";
try
{
zip(dir, zipPath, zipFileName);
}
catch (Exception e)
{
e.printStackTrace();
}
String zipFilePath = "D:\\ziptest\\zipPath\\test.zip";
String unzipFilePath = "D:\\ziptest\\zipPath";
try
{
unzip(zipFilePath, unzipFilePath, true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
简单java解压缩问题
既然目录的问题那你可以在输出流输出之前创建目录撒,
你先创建一个File对象
然后测试他是否为一个目录,不是就创建目录
如你hello.zip
File f=new File("E:\\hello");
if(!f.isDirectory()){
f.mkdirs();
}
关于Java的解压缩的代码?
package com.javatest.techzero.gui;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipFileDemo {
@SuppressWarnings("resource")
public static void main(String args[]) throws Exception {
File file = new File("d:" + File.separator + "test.zip");
File outFile = null;
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream out = null;
while ((entry = zipInput.getNextEntry()) != null) {
System.out.println("开始解压缩" + entry.getName() + "文件。。。");
outFile = new File("d:" + File.separator + entry.getName());
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdir();
}
if (!outFile.exists()) {
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
out = new FileOutputStream(outFile);
int temp = 0;
while ((temp = input.read()) != -1) {
SPAN style="WHITE-SPACE: pre" /SPAN//System.out.println(temp);
out.write(temp);
}
input.close();
out.close();
}
System.out.println("Done!");
}
}
仅供参考
java解压zip文件
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
/**
* 获得zip文件里的所有文件
* @author Administrator
*
*/
public class ZipFile {
public ZipFile() throws IOException
{
java.util.zip.ZipFile zf = new java.util.zip.ZipFile("E:/Java/Project.zip");
Enumeration e = zf.entries();
while(e.hasMoreElements())
{
ZipEntry ze = (ZipEntry) e.nextElement();
if(!ze.isDirectory())
System.out.println(new String(ze.getName().getBytes("ISO-8859-1"), "GB2312"));
}
}
public static void main(String[] args) {
try {
new ZipFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
java 解压缩之后文件内容读取
您的排序与zip排序不一样。
所以第一步应该得到所有名称,然后再按照新的顺序来读
如果只是解压到某处,新的顺序一点用处都没有!
新闻名称:java解析压缩文件代码,java代码怎么压缩zip文件
标题路径:http://scgulin.cn/article/hdisoo.html