资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

javazip压缩代码 java 压缩

如何用java 将文件加密压缩为zip文件.

用java加密压缩zip文件:

目前创新互联已为数千家的企业提供了网站建设、域名、网站空间网站托管运营、企业网站设计、姑苏网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

package com.ninemax.demo.zip.decrypt;

import java.io.File;

import java.io.IOException;

import java.util.List;

import java.util.zip.DataFormatException;

import org.apache.commons.io.FileUtils;

import de.idyl.winzipaes.AesZipFileDecrypter;

import de.idyl.winzipaes.AesZipFileEncrypter;

import de.idyl.winzipaes.impl.AESDecrypter;

import de.idyl.winzipaes.impl.AESDecrypterBC;

import de.idyl.winzipaes.impl.AESEncrypter;

import de.idyl.winzipaes.impl.AESEncrypterBC;

import de.idyl.winzipaes.impl.ExtZipEntry;

/**

* 压缩指定文件或目录为ZIP格式压缩文件

* 支持中文(修改源码后)

* 支持密码(仅支持256bit的AES加密解密)

* 依赖bcprov项目(bcprov-jdk16-140.jar)

*

* @author zyh

*/

public class DecryptionZipUtil {

/**

* 使用指定密码将给定文件或文件夹压缩成指定的输出ZIP文件

* @param srcFile 需要压缩的文件或文件夹

* @param destPath 输出路径

* @param passwd 压缩文件使用的密码

*/

public static void zip(String srcFile,String destPath,String passwd) {

AESEncrypter encrypter = new AESEncrypterBC();

AesZipFileEncrypter zipFileEncrypter = null;

try {

zipFileEncrypter = new AesZipFileEncrypter(destPath, encrypter);

/**

* 此方法是修改源码后添加,用以支持中文文件名

*/

zipFileEncrypter.setEncoding("utf8");

File sFile = new File(srcFile);

/**

* AesZipFileEncrypter提供了重载的添加Entry的方法,其中:

* add(File f, String passwd)

* 方法是将文件直接添加进压缩文件

*

* add(File f, String pathForEntry, String passwd)

* 方法是按指定路径将文件添加进压缩文件

* pathForEntry - to be used for addition of the file (path within zip file)

*/

doZip(sFile, zipFileEncrypter, "", passwd);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

zipFileEncrypter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 具体压缩方法,将给定文件添加进压缩文件中,并处理压缩文件中的路径

* @param file 给定磁盘文件(是文件直接添加,是目录递归调用添加)

* @param encrypter AesZipFileEncrypter实例,用于输出加密ZIP文件

* @param pathForEntry ZIP文件中的路径

* @param passwd 压缩密码

* @throws IOException

*/

private static void doZip(File file, AesZipFileEncrypter encrypter,

String pathForEntry, String passwd) throws IOException {

if (file.isFile()) {

pathForEntry += file.getName();

encrypter.add(file, pathForEntry, passwd);

return;

}

pathForEntry += file.getName() + File.separator;

for(File subFile : file.listFiles()) {

doZip(subFile, encrypter, pathForEntry, passwd);

}

}

/**

* 使用给定密码解压指定压缩文件到指定目录

* @param inFile 指定Zip文件

* @param outDir 解压目录

* @param passwd 解压密码

*/

public static void unzip(String inFile, String outDir, String passwd) {

File outDirectory = new File(outDir);

if (!outDirectory.exists()) {

outDirectory.mkdir();

}

AESDecrypter decrypter = new AESDecrypterBC();

AesZipFileDecrypter zipDecrypter = null;

try {

zipDecrypter = new AesZipFileDecrypter(new File(inFile), decrypter);

AesZipFileDecrypter.charset = "utf-8";

/**

* 得到ZIP文件中所有Entry,但此处好像与JDK里不同,目录不视为Entry

* 需要创建文件夹,entry.isDirectory()方法同样不适用,不知道是不是自己使用错误

* 处理文件夹问题处理可能不太好

*/

ListExtZipEntry entryList = zipDecrypter.getEntryList();

for(ExtZipEntry entry : entryList) {

String eName = entry.getName();

String dir = eName.substring(0, eName.lastIndexOf(File.separator) + 1);

File extractDir = new File(outDir, dir);

if (!extractDir.exists()) {

FileUtils.forceMkdir(extractDir);

}

/**

* 抽出文件

*/

File extractFile = new File(outDir + File.separator + eName);

zipDecrypter.extractEntry(entry, extractFile, passwd);

}

} catch (IOException e) {

e.printStackTrace();

} catch (DataFormatException e) {

e.printStackTrace();

} finally {

try {

zipDecrypter.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 测试

* @param args

*/

public static void main(String[] args) {

/**

* 压缩测试

* 可以传文件或者目录

*/

// zip("M:\\ZIP\\test\\bb\\a\\t.txt", "M:\\ZIP\\test\\temp1.zip", "zyh");

// zip("M:\\ZIP\\test\\bb", "M:\\ZIP\\test\\temp2.zip", "zyh");

unzip("M:\\ZIP\\test\\temp2.zip", "M:\\ZIP\\test\\temp", "zyh");

}

}

压缩多个文件时,有两个方法(第一种没试):

(1) 预先把多个文件压缩成zip,然后调用enc.addAll(inZipFile, password);方法将多个zip文件加进来。

(2)针对需要压缩的文件循环调用enc.add(inFile, password);,每次都用相同的密码。

如何使用java压缩文件夹成为zip包(最简单的

import java.io.File;

public class ZipCompressorByAnt {

private File zipFile;

/**

* 压缩文件构造函数

* @param pathName 最终压缩生成的压缩文件:目录+压缩文件名.zip

*/

public ZipCompressorByAnt(String finalFile) {

zipFile = new File(finalFile);

}

/**

* 执行压缩操作

* @param srcPathName 需要被压缩的文件/文件夹

*/

public void compressExe(String srcPathName) {

System.out.println("srcPathName="+srcPathName);

File srcdir = new File(srcPathName);

if (!srcdir.exists()){

throw new RuntimeException(srcPathName + "不存在!");

}

Project prj = new Project();

Zip zip = new Zip();

zip.setProject(prj);

zip.setDestFile(zipFile);

FileSet fileSet = new FileSet();

fileSet.setProject(prj);

fileSet.setDir(srcdir);

//fileSet.setIncludes("**/*.java"); //包括哪些文件或文件夹 eg:zip.setIncludes("*.java");

//fileSet.setExcludes(...); //排除哪些文件或文件夹

zip.addFileset(fileSet);

zip.execute();

}    

}

public class TestZip {

public static void main(String[] args) {

ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\test1.zip ");

zca.compressExe("E:\\test1");

}  

}

/*如果 出现ant 的 52  51 50 等版本问题 可以去找对应的ant-1.8.2.jar     我开始用的ant-1.10.1.jar 就是这个包版本高了  一直报verson 52 版本问题*/

java代码实现 导出zip包,无法打开zip压缩包

package com.lch.test;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

public class ZIP {

public static void main(String[] argv) throws Exception {

ZipFile zf = new ZipFile("E:\\wk\\LBSLEMIS201106141057\\LBSLEMIS\\test\\com\\lch\\test\\filename.zip");

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {

String zipEntryName = ((ZipEntry) entries.nextElement()).getName();

System.out.println(zipEntryName);

}

}

}

用javad 的ZipFile类的ZipEntry方法试一下 找到ZIP里面的ZipEntry方法 读取Zip里面压缩文件的内容

有可能会引用外包

你好,我不知道你说的dzp是什么格式文件,但如果是zip的压缩文件,可以看下我的这段代码

ZipFile file = new ZipFile("d:\\1.zip");

ZipEntry entry = file.getEntry("1.xml"); //假如压缩包里的文件名是1.xml

InputStream in=file.getInputStream(entry);

最后就是按照java中一贯的流的处理方式即可

可以不解压,zip包里的一个对象就是一个ZipEntry

找到你想要的那个ZipEntry,用文流写出来就可以了。追问通过ZipEntry,然后用流就可以读出里面的内容了吗?谢谢指点!

回答/**

* 解压

* @param root 输出目标

* @param zipfile zip文件

*/

protected void unzip(File root, File zipfile, String file) throws Exception {

// 解压文件不存在时返回

if (!zipfile.exists()) {

return;

}

// 释放目录不存时创建

if (!root.exists()) {

root.mkdirs();

}

// 释放目录不为目录时返回

if (!root.isDirectory()) {

return;

}

FileInputStream fin = new FileInputStream(zipfile);

ZipInputStream zin = new ZipInputStream(fin);

ZipEntry entry = null;

while ((entry = zin.getNextEntry()) != null) {

// if (!entry.getName().endsWith(file)) {

// continue;

// }

File tmp = new File(root, entry.getName());

if (entry.isDirectory()) {

tmp.mkdirs();

} else {

byte[] buff = new byte[4096];

int len = 0;

tmp.getParentFile().mkdirs();

FileOutputStream fout = new FileOutputStream(tmp);

while ((len = zin.read(buff)) != -1) {

fout.write(buff, 0, len);

}

zin.closeEntry();

fout.close();

}

}

}

这里完整的解压代码。

// if (!entry.getName().endsWith(file)) {

// continue;

// }

这段打开就是只解出一个你指定的文件。

下面是测试用的。

public static void main(String[] args) throws Exception {

new CommonFiles().unzip(new File("D:\\"), new File("D:\\test.zip"),"file.txt");

}

这个例子会在D盘生成型个test文件夹,file.txt就会在里面,(里面也可能会有多个文件夹,这个取决于压缩包里文件的度)

如何使用java压缩文件夹成为zip包

在JDK中有一个zip工具类:

java.util.zip    Provides classes for reading and writing the standard ZIP and

GZIP file formats.

使用此类可以将文件夹或者多个文件进行打包压缩操作。

在使用之前先了解关键方法:

ZipEntry(String name)         Creates a new zip entry with the specified name.

使用ZipEntry的构造方法可以创建一个zip压缩文件包的实例,然后通过ZipOutputStream将待压缩的文件以流的形式写进该压缩包中。具体实现代码如下:

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.length1){  

System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");  

}else{  

fos = new FileOutputStream(zipFile);  

zos = new ZipOutputStream(new BufferedOutputStream(fos));  

byte[] bufs = new byte[1024*10];  

for(int i=0;isourceFiles.length;i++){  

//创建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("文件打包失败!");  

}  

}  

}


网页题目:javazip压缩代码 java 压缩
地址分享:http://cdkjz.cn/article/dosdhds.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220