在java中如何把一个文件压缩成zip格式的文件,并且有加密

懒萝卜 2010-01-04 01:07:13
在java中如何把一个文件压缩成zip格式的文件,并且有加密

压缩成zip格式的文件我已经实现,
就是不知道如何把密码给弄进去,
并且压缩后的zip文件,可以用目前的WinRAR,WinZip等压缩软件打开,当然在打开的时候得输入密码了。
...全文
1670 14 打赏 收藏 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuaj1980 2011-05-16
  • 打赏
  • 举报
回复
在codeproject上能找到源代码,但哥看不懂,感觉很底层的东西。555
Anssin007 2010-12-30
  • 打赏
  • 举报
回复
我也遇上了同样的问题,没有头绪呀
meadking 2010-01-07
  • 打赏
  • 举报
回复
你要加密码是吗?
直接用7zip的dll调用就好了
cmd命令行,在java里面调用...呵呵原理一样的
懒萝卜 2010-01-07
  • 打赏
  • 举报
回复
没有一个是符合我的
目前在网上看到的是可以加密,但是不能用客户端打开,只能用程序自己打开。
但是这种结果肯定是不正确的。
SambaGao 2010-01-04
  • 打赏
  • 举报
回复
有一本书叫 java 实用组件集 上面有好多控件。
bobo415 2010-01-04
  • 打赏
  • 举报
回复
那个加密时压缩完后加的密吧, 有加密软件
有点印象.
meadking 2010-01-04
  • 打赏
  • 举报
回复
http://code.google.com/p/i18n-mead-properties-plugin/source/browse/trunk/source/src/meadproperty/util/ZipUtil.java

代码管理在这里哦...
meadking 2010-01-04
  • 打赏
  • 举报
回复

package meadproperty.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

public static int BUFFER_BYTE = 512;

private static String BASE_DIR = "";
private static byte BYTES[] = new byte[BUFFER_BYTE];

public static void zipDir(String src_dir) throws IOException {
zipDir(src_dir, src_dir + ".zip");
}

/**
*
* @param src_dir
* @param dest_zip
* @throws IOException
*/
public static void zipDir(String src_dir, String dest_zip)
throws IOException {

FileOutputStream fos = new FileOutputStream(dest_zip);
ZipOutputStream zout = new ZipOutputStream(fos);
File file = new File(src_dir);
if (!file.exists()) {
// src not available...
System.err.println(file.getName() + " is not available!");
return;
} else if (!file.isDirectory()) {
zip(zout, file, file.getPath());
System.err.println(file.getName()
+ "#is file, not dir!warn, but no error caused!");
}
zip(zout, file, "");

zout.close();
fos.close();
}

/**
*
* @param zipOut
* @param file
* @param base
* @throws IOException
*/
protected static void zip(ZipOutputStream zipOut, File file, String base)
throws IOException {
if (file.isDirectory()) {
// process dir
File[] fl = file.listFiles();
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(zipOut, fl[i], base + fl[i].getName());
}
} else {
// process file
zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
InputStream in = new FileInputStream(file);
System.out.println(base);
int len = 0;
while ((len = in.read(BYTES)) != -1) {
zipOut.write(BYTES, 0, len);
}
in.close();
}
}

/**
* unZip the file
*
* @param fileName
* @throws IOException
*/
public static void unzipFile(String src_zip) throws IOException {
int i = src_zip.lastIndexOf('.');
String dest = src_zip.substring(0, i);
unzipFile(src_zip, dest);
}

/**
*
* @param src_zip
* @param dest_dir
* @throws IOException
*/
public static void unzipFile(String src_zip, String dest_dir)
throws IOException {
ZipUtil.BASE_DIR = dest_dir;
File base = new File(ZipUtil.BASE_DIR);
if (!base.exists()) {
base.mkdirs();
}
InputStream in = new BufferedInputStream(new FileInputStream(src_zip));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
try {
unzip(zin, ZipUtil.BASE_DIR + File.separator + entry.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
zin.close();
}

/**
*
* @param zin
* @param entryName
* the spe file entry in the zip file
* @throws IOException
*/
protected static void unzip(ZipInputStream zin, String entryName)
throws IOException {
System.out.println("unzipping " + entryName);
// create dir
if (entryName.endsWith("/")) {
System.out.println("INFO: #endsWith/,will return now");
return;
}
int i = entryName.lastIndexOf('/');
if (i > 0) {
String path = entryName.substring(0, i);
File file = new File(path);
if (!file.exists()) {
System.out.println(file.getName()
+ "INFO: #not existed, will create one");
file.mkdirs();
}
System.out.println(path + " =file= " + i);
}
// create file
FileOutputStream out = new FileOutputStream(entryName);
int len = 0;
while ((len = zin.read(BYTES)) != -1) {
out.write(BYTES, 0, len);
}
out.close();
}

}




支持多级目录..多个文件
2到20个英文 2010-01-04
  • 打赏
  • 举报
回复
这个是写比较小的文件,写大文件方法楼主自己优化
2到20个英文 2010-01-04
  • 打赏
  • 举报
回复


import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* @author dqhe
* 压缩zip
* 解压zip
*/
public class ZipCompress {

private static final int BSIZE = 1024 * 1000 ;
/**
* @param args
*/
public static void main(String[] args)throws Exception{
FileOutputStream file = new FileOutputStream("E://he.zip");

//Adler32(更快一些)和CRC32(慢一点但比较准确)
CheckedOutputStream cOut = new CheckedOutputStream(file,new Adler32());
ZipOutputStream zipOut = new ZipOutputStream(cOut);
BufferedOutputStream out = new BufferedOutputStream(zipOut);
zipOut.setComment("this is a java zipping");

//写两个同样的文件进入
for(int i=0;i<2;i++)
{
FileChannel in = new FileInputStream("E://a.txt").getChannel();
zipOut.putNextEntry(new ZipEntry("c"+i+".txt"));
ByteBuffer buff = ByteBuffer.allocate(BSIZE);

//BufferedReader in = new BufferedReader(new FileReader("E://c.txt"));
//int c;
System.out.println("正在写入,请稍等……");
while(in.read(buff) != -1)
{
System.out.println(in.read(buff));
buff.flip();
out.write(buff.array());
buff.clear();
}
System.out.println("成功写入。");
}
out.close();
zipOut.close();
cOut.close();
}

}


以前写的楼主可以参考下压缩的,密码没写过
有可能就加一个属性吧,呵呵
@井九 2010-01-04
  • 打赏
  • 举报
回复
up
Bleibo 2010-01-04
  • 打赏
  • 举报
回复
第一次,听说,顶,如果楼主找到了解决方法,请共享一下,呵呵
rumlee 2010-01-04
  • 打赏
  • 举报
回复
jdk里就有zip包,很容易的。

至于加密,如果最终也是通过自己的程序来解压倒是没有问题,如果用rar之类的工具来解压的化,那这个加密不知道java能不能实现。你可以看看jdk的api看看。
szvsking 2010-01-04
  • 打赏
  • 举报
回复
传说中的沙发

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧