资讯

精准传达 • 有效沟通

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

java文件压缩核心代码 java 文件压缩算法

求一个JAVA的压缩程序源代码。

package com.io2.homework;

在商城等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、成都做网站 网站设计制作按需制作网站,公司网站建设,企业网站建设,高端网站设计,成都全网营销,外贸网站制作,商城网站建设费用合理。

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;

/*压缩文件夹*/

public class MyMultipleFileZip

{

private String currentZipFilePath = "F:/MyZip.zip";

private String sourceFilePath;

private ZipOutputStream zos;

private FileInputStream fis;

public MyMultipleFileZip(String sourceFilePath)

{

try

{

this.sourceFilePath = sourceFilePath;

zos = new ZipOutputStream(new FileOutputStream(currentZipFilePath));

//设定文件压缩级别

zos.setLevel(9);

} catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

// 在当前条目中写入具体内容

public void writeToEntryZip(String filePath)

{

try

{

fis = new FileInputStream(filePath);

} catch (FileNotFoundException e1)

{

e1.printStackTrace();

}

byte[] buff = new byte[1024];

int len = 0;

try

{

while ((len = fis.read(buff)) != -1)

{

zos.write(buff, 0, len);

}

} catch (IOException e)

{

e.printStackTrace();

}finally

{

if (fis != null)

try

{

fis.close();

} catch (IOException e)

{

e.printStackTrace();

}

}

}

// 添加文件条目

public void addFileEntryZip(String fileName)

{

try

{

zos.putNextEntry(new ZipEntry(fileName));

} catch (IOException e)

{

e.printStackTrace();

}

}

public void addDirectoryEntryZip(String directoryName)

{

try

{

zos.putNextEntry(new ZipEntry(directoryName + "/"));

} catch (IOException e)

{

e.printStackTrace();

}

}

// 遍历文件夹

public void listMyDirectory(String filePath)

{

File f = new File(filePath);

File[] files = f.listFiles();

if(files!=null)

{

for (File currentFile : files)

{

// 设置条目名称(此步骤非常关键)

String entryName= currentFile.getAbsolutePath().split(":")[1].substring(1);

// 获取文件物理路径

String absolutePath = currentFile.getAbsolutePath();

if (currentFile.isDirectory())

{

addDirectoryEntryZip(entryName);

//进行递归调用

listMyDirectory(absolutePath);

}

else

{

addFileEntryZip(entryName);

writeToEntryZip(absolutePath);

}

}

}

}

// 主要流程

public void mainWorkFlow()

{

listMyDirectory(this.sourceFilePath);

if(zos!=null)

try

{

zos.close();

} catch (IOException e)

{

e.printStackTrace();

}

}

public static void main(String[] args)

{

new MyMultipleFileZip("F:/fountainDirectory").mainWorkFlow();

}

}

如何将文件夹压缩成JAR文件

eclipse有相关操作

------------------------------------------------------------------------

1,右击Eclipse项目,点击Export。

2,选中java-jar文件。

3,选中要发布为jar文件的项目。

1)选中源文件夹,库文件夹,配置文件文件夹。

2)不要选中Eclipse项目的文件和其他不需要的文件。

3)选中:Export all output folders for checked projects

输出所有选中的文件夹。

4)选中:Export java source files and resources

如果是源文件夹,就会输出生成的.class文件。

5)选中:压缩Jar文件的内容。

6)选中:不警告就覆盖已存在的文件

7)选择一个输出jar文件的目的地。

4,使用已存在的manifest文件。

5,MANIFEST.MF 文件如下:

Manifest-Version: 1.0

Main-Class: Test

Class-Path: lib/OXmlEd1.11-nolib-bin.jar lib/dom4j-1.6.1.jar lib/commons-logging-1.0.4.jar lib/log4j-1.2.8.jar

如果让Eclipse为我们生成,不会生成Class-Path部分的描述。所以,我们需要再加上Class-Path描述!

6,现在,制作完成的jar文件就是可执行的。

我们在命令行中执行java –jar AAA.jar 就可以看到该程序被成功执行了。

javaw –jar AAA.jar 也可以执行该jar包。

使用java.exe将会出现命令行窗口。

使用javaw.exe执行,不会出现命令行窗口,所有System.out.print这样的输出都是看不到的。

7,如果你安装了JRE,你的jar文件应该关联在javaw –jar上。

如果你的jar文件没有关联javaw –jar了,可以通过以下方法重新关联。

1)在资源窗口—工具—文件夹选项—文件类型中,新建jar的关联。

2)创建一个.bat文件,其中的命令是:javaw -jar %1 或者java -jar %1

3)把.jar文件关联到上面这样的.bat文件中。

这样,你双击.jar文件时,就会立刻执行该java程序。

%1 表示第一个参数,也就是你点击的jar文件的文件全名。

8,你也可以为每一个可执行的jar文件创建一个.bat文件,其中的命令是javaw -jar AAA.jar 或者java –jar AAA.jar等文件。

这样,无需关联jar文件,就可以执行jar文件。

关于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文件.

用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点抗 mons.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文件压缩核心代码 java 文件压缩算法
文章地址:http://cdkjz.cn/article/ddehdop.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220