java解包_java中打包和解包

package com.test.xml;

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;

public class ZipManager {

/**

*

* @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 = 0; i < 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 sourceFileName

* 所要压缩的文件名

* @param zipFilename

* 压缩后的文件名

* @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();

}

/**

* 压缩多个文件

* @param zipFileName

* 要压缩后的压缩文件名

* @param lstFileName

* 要压缩的文件名集合

*

* @return 压缩后文件大小

* @throws Exception

*/

public long createFileToZip(String zipFileName, ListlstFileName) throws Exception {

File objFile = new File(zipFileName);

ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(

objFile));

if (lstFileName != null) {

for (int i = 1; i < lstFileName.size(); i++) {

File sourceFile = new File(lstFileName.get(i));

byte[] buf = new byte[1024];

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 sourceFile

* 所要压缩的文件名

* @param zipFile

* 压缩后的文件名

* @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();

}

/**

* @param sourceZip

* 要解压的文件名

* @param ourFileName

* 解压后的文件路径

*

* @throws IOException

*/

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 = 0; i < tmp.length; i++) {

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 = 0; i < dirs.length - 1; i++) {

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();

long l = 0;

try {

//manager.createZip("E:\\Test Zip", "E:\\Test Zip.rar");

manager.releaseZipToFile("E:\\Test Zip.rar", "E:\\Test");

} catch (Exception e) {

}

System.out.println("Result=" + l);

}

}

Java编程可以利用`java.util.jar`下的`JarFile`和`JarEntry`类来实现jar的解包打包。 1. 解包jar文件: ```java import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class JarUnpacker { public static void unpack(String jarPath, String destPath) throws Exception { JarFile jarFile = new JarFile(jarPath); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); String fileName = jarEntry.getName(); File destFile = new File(destPath + File.separator + fileName); if (jarEntry.isDirectory()) { destFile.mkdirs(); } else { InputStream in = jarFile.getInputStream(jarEntry); OutputStream out = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); } } jarFile.close(); } } ``` 以上代码会将`jarPath`指定的jar文件解压到`destPath`指定的目录。 2. 打包jar文件: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; public class JarPacker { public static void pack(String sourcePath, String jarPath) throws Exception { JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarPath)); File sourceFile = new File(sourcePath); if (sourceFile.isDirectory()) { packDir(sourceFile, "", jarOut); } else { packFile(sourceFile, "", jarOut); } jarOut.close(); } private static void packDir(File dir, String parentPath, JarOutputStream jarOut) throws IOException { String dirPath = parentPath + dir.getName() + "/"; JarEntry jarEntry = new JarEntry(dirPath); jarOut.putNextEntry(jarEntry); File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { packDir(file, dirPath, jarOut); } else { packFile(file, dirPath, jarOut); } } } } private static void packFile(File file, String parentPath, JarOutputStream jarOut) throws IOException { JarEntry jarEntry = new JarEntry(parentPath + file.getName()); jarOut.putNextEntry(jarEntry); byte[] buffer = new byte[1024]; int len; FileInputStream in = new FileInputStream(file); while ((len = in.read(buffer)) > 0) { jarOut.write(buffer, 0, len); } in.close(); } } ``` 以上代码会将`sourcePath`指定的文件或目录打包成`jarPath`指定的jar文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值