import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
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.ZipOutputStream
import java.util.zip.GZIPInputStream
import java.io.DataInputStream
/**
* Description: 此类用于...
*
* @authorwunaigang(2005-6-21)
* @version 1.0.0
*/
public class ZipManager {
/**
* zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.
*
* @param baseDir 所要压缩的目录名(包含绝对路径)
* @param objFileName 压缩后的文件名
* @throws Exception
*/
public void createZip(String baseDir, String objFileName) throws Exception {
File folderObject = new File(baseDir)
if (folderObject.exists()){
List fileList = getSubFiles(new File(baseDir))
//压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName))
ZipEntry ze = null
byte[] buf = new byte[1024]
int readLen = 0
for (int i = 0i <fileList.size()i++) {
File f = (File) fileList.get(i)
System.out.println("Adding: " + f.getPath() + f.getName())
//创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(baseDir, f))
ze.setSize(f.length())
ze.setTime(f.lastModified())
//将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze)
InputStream is = new BufferedInputStream(new FileInputStream(f))
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen)
}
is.close()
System.out.println("done...")
}
zos.close()
}else{
throw new Exception("this folder isnot exist!")
}
}
/**
* zip压缩功能测试. 将指定文件压缩后存到一压缩文件中
*
* @param baseDir 所要压缩的文件名
* @param objFileName 压缩后的文件名
* @return 压缩后文件的大小
* @throws Exception
*/
public long createFileToZip(String zipFilename,String sourceFileName) throws Exception {
File sourceFile = new File(sourceFileName)
byte[] buf = new byte[1024]
//压缩文件名
File objFile = new File(zipFilename)
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile))
ZipEntry ze = null
//创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(sourceFile.getName())
ze.setSize(sourceFile.length())
ze.setTime(sourceFile.lastModified())
//将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze)
InputStream is = new BufferedInputStream(new FileInputStream(sourceFile))
int readLen = -1
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen)
}
is.close()
zos.close()
return objFile.length()
}
/**
* zip压缩功能测试. 将指定文件压缩后存到一压缩文件中
*
* @param baseDir 所要压缩的文件名
* @param objFileName 压缩后的文件名
* @return 压缩后文件的大小
* @throws Exception
*/
public long createFileToZip(File sourceFile,File zipFile)throws IOException {
byte[] buf = new byte[1024]
//压缩文件名
File objFile = zipFile
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile))
ZipEntry ze = null
//创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(sourceFile.getName())
ze.setSize(sourceFile.length())
ze.setTime(sourceFile.lastModified())
//将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze)
InputStream is = new BufferedInputStream(new FileInputStream(sourceFile))
int readLen = -1
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen)
}
is.close()
zos.close()
return objFile.length()
}
/**
* 测试解压缩功能. 将d:\\download\\test.zip连同子目录解压到d:\\temp\\zipout目录下.
*
* @throws Exception
*/
public void releaseZipToFile(String sourceZip, String outFileName)
throws IOException{
ZipFile zfile=new ZipFile(sourceZip)
System.out.println(zfile.getName())
Enumeration zList=zfile.entries()
ZipEntry ze=null
byte[] buf=new byte[1024]
while(zList.hasMoreElements()){
//从ZipFile中得到一个ZipEntry
ze=(ZipEntry)zList.nextElement()
if(ze.isDirectory()){
continue
}
//以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())))
InputStream is=new BufferedInputStream(zfile.getInputStream(ze))
int readLen=0
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen)
}
is.close()
os.close()
System.out.println("Extracted: "+ze.getName())
}
zfile.close()
}
/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir
*File 指定的目录
* @return 包含java.io.File的List
*/
private List getSubFiles(File baseDir) {
List ret = new ArrayList()
//File base=new File(baseDir)
File[] tmp = baseDir.listFiles()
for (int i = 0i <tmp.lengthi++) {
if (tmp[i].isFile()) {
ret.add(tmp[i])
}
if (tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]))
}
}
return ret
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param baseDir
*指定根目录
* @param absFileName
*相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/")
//System.out.println(dirs.length)
File ret = new File(baseDir)
//System.out.println(ret)
if (dirs.length >1) {
for (int i = 0i <dirs.length - 1i++) {
ret = new File(ret, dirs[i])
}
}
if (!ret.exists()) {
ret.mkdirs()
}
ret = new File(ret, dirs[dirs.length - 1])
return ret
}
/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
* @param baseDir
*java.lang.String 根目录
* @param realFileName
*java.io.File 实际的文件名
* @return 相对文件名
*/
private String getAbsFileName(String baseDir, File realFileName) {
File real = realFileName
File base = new File(baseDir)
String ret = real.getName()
while (true) {
real = real.getParentFile()
if (real == null)
break
if (real.equals(base))
break
else {
ret = real.getName() + "/" + ret
}
}
System.out.println("TTTTT" + ret)
return ret
}
public void testReadZip() throws Exception{
String baseDir="d:\\temp\\zipout"
ZipFile zfile=new ZipFile("d:\\download\\src.zip")
System.out.println(zfile.getName())
Enumeration zList=zfile.entries()
ZipEntry ze=null
byte[] buf=new byte[1024]
while(zList.hasMoreElements()){
//从ZipFile中得到一个ZipEntry
ze=(ZipEntry)zList.nextElement()
if(ze.isDirectory()){
continue
}
//以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(baseDir, ze.getName())))
InputStream is=new BufferedInputStream(zfile.getInputStream(ze))
int readLen=0
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen)
}
is.close()
os.close()
System.out.println("Extracted: "+ze.getName())
}
zfile.close()
}
public static void main(String args[]){
ZipManager manager = new ZipManager()
try {
//manager.releaseZipToFile("c:\\test.zip","c:\\test")
manager.testReadZip()
}
catch (Exception e) {}
System.out.println("over")
}
}
1.代码如下:[java] view plain copy
<span style="font-size:18pxbackground-color: rgb(204, 204, 204)">package cn.gov.csrc.base.util
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*
*/
public final class FileToZip {
private FileToZip(){}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false
File sourceFile = new File(sourceFilePath)
FileInputStream fis = null
BufferedInputStream bis = null
FileOutputStream fos = null
ZipOutputStream zos = null
if(sourceFile.exists() == false){
System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.")
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip")
if(zipFile.exists()){
System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.")
}else{
File[] sourceFiles = sourceFile.listFiles()
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.")
}else{
fos = new FileOutputStream(zipFile)
zos = new ZipOutputStream(new BufferedOutputStream(fos))
byte[] bufs = new byte[1024*10]
for(int i=0i<sourceFiles.lengthi++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName())
zos.putNextEntry(zipEntry)
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i])
bis = new BufferedInputStream(fis, 1024*10)
int read = 0
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read)
}
}
flag = true
}
}
} catch (FileNotFoundException e) {
e.printStackTrace()
throw new RuntimeException(e)
} catch (IOException e) {
e.printStackTrace()
throw new RuntimeException(e)
} finally{
//关闭流
try {
if(null != bis) bis.close()
if(null != zos) zos.close()
} catch (IOException e) {
e.printStackTrace()
throw new RuntimeException(e)
}
}
}
return flag
}
public static void main(String[] args){
String sourceFilePath = "D:\\TestFile"
String zipFilePath = "D:\\tmp"
String fileName = "12700153file"
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName)
if(flag){
System.out.println("文件打包成功!")
}else{
System.out.println("文件打包失败!")
}
}
}
</span>
2.结果如下:
文件打包成功!
3.到D:/tmp下查看,你会发现生成了一个zip压缩包.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)