java文件壓縮核心代碼 java 文件壓縮算法

求一個JAVA的壓縮程序源代碼。

package com.io2.homework;

在商城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,成都全網(wǎng)營銷,外貿(mào)網(wǎng)站制作,商城網(wǎng)站建設(shè)費(fèi)用合理。

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

//設(shè)定文件壓縮級別

zos.setLevel(9);

} catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

// 在當(dāng)前條目中寫入具體內(nèi)容

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)

{

// 設(shè)置條目名稱(此步驟非常關(guān)鍵)

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

// 獲取文件物理路徑

String absolutePath = currentFile.getAbsolutePath();

if (currentFile.isDirectory())

{

addDirectoryEntryZip(entryName);

//進(jìn)行遞歸調(diào)用

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有相關(guān)操作

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

1,右擊Eclipse項目,點(diǎn)擊Export。

2,選中java-jar文件。

3,選中要發(fā)布為jar文件的項目。

1)選中源文件夾,庫文件夾,配置文件文件夾。

2)不要選中Eclipse項目的文件和其他不需要的文件。

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

輸出所有選中的文件夾。

4)選中:Export java source files and resources

如果是源文件夾,就會輸出生成的.class文件。

5)選中:壓縮Jar文件的內(nèi)容。

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,現(xiàn)在,制作完成的jar文件就是可執(zhí)行的。

我們在命令行中執(zhí)行java –jar AAA.jar 就可以看到該程序被成功執(zhí)行了。

javaw –jar AAA.jar 也可以執(zhí)行該jar包。

使用java.exe將會出現(xiàn)命令行窗口。

使用javaw.exe執(zhí)行,不會出現(xiàn)命令行窗口,所有System.out.print這樣的輸出都是看不到的。

7,如果你安裝了JRE,你的jar文件應(yīng)該關(guān)聯(lián)在javaw –jar上。

如果你的jar文件沒有關(guān)聯(lián)javaw –jar了,可以通過以下方法重新關(guān)聯(lián)。

1)在資源窗口—工具—文件夾選項—文件類型中,新建jar的關(guān)聯(lián)。

2)創(chuàng)建一個.bat文件,其中的命令是:javaw -jar %1 或者java -jar %1

3)把.jar文件關(guān)聯(lián)到上面這樣的.bat文件中。

這樣,你雙擊.jar文件時,就會立刻執(zhí)行該java程序。

%1 表示第一個參數(shù),也就是你點(diǎn)擊的jar文件的文件全名。

8,你也可以為每一個可執(zhí)行的jar文件創(chuàng)建一個.bat文件,其中的命令是javaw -jar AAA.jar 或者java –jar AAA.jar等文件。

這樣,無需關(guān)聯(lián)jar文件,就可以執(zhí)行jar文件。

關(guān)于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點(diǎn)抗 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)

* 方法是將文件直接添加進(jìn)壓縮文件

*

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

* 方法是按指定路徑將文件添加進(jìn)壓縮文件

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

}

}

}

/**

* 具體壓縮方法,將給定文件添加進(jìn)壓縮文件中,并處理壓縮文件中的路徑

* @param file 給定磁盤文件(是文件直接添加,是目錄遞歸調(diào)用添加)

* @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

* 需要創(chuàng)建文件夾,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) 預(yù)先把多個文件壓縮成zip,然后調(diào)用enc.addAll(inZipFile, password);方法將多個zip文件加進(jìn)來。

(2)針對需要壓縮的文件循環(huán)調(diào)用enc.add(inFile, password);,每次都用相同的密碼。

網(wǎng)站名稱:java文件壓縮核心代碼 java 文件壓縮算法
網(wǎng)站網(wǎng)址:http://bm7419.com/article8/ddehdop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站定制網(wǎng)站、外貿(mào)建站App開發(fā)、網(wǎng)頁設(shè)計公司、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)