本文目錄一覽:
- 1、怎樣用Java生成ZIP文件
- 2、如何使用JAVA代碼壓縮PDF文件
- 3、如何使用java壓縮文件夾成為zip包
- 4、java中的壓縮原理是什麼?
- 5、如何使用java壓縮文件夾成為zip包(最簡單的
- 6、如何用JAVA把內存里的二進位文件打包成ZIP包
怎樣用Java生成ZIP文件
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* @project: Test
* @author chenssy
* @date 2013-7-28
* @Description: 文件壓縮工具類
* 將指定文件/文件夾壓縮成zip、rar壓縮文件
*/
public class CompressedFileUtil {
/**
* 默認構造函數
*/
public CompressedFileUtil(){
}
/**
* @desc 將源文件/文件夾生成指定格式的壓縮文件,格式zip
* @param resourePath 源文件/文件夾
* @param targetPath 目的壓縮文件保存路徑
* @return void
* @throws Exception
*/
public void compressedFile(String resourcesPath,String targetPath) throws Exception{
File resourcesFile = new File(resourcesPath); //源文件
File targetFile = new File(targetPath); //目的
//如果目的路徑不存在,則新建
if(!targetFile.exists()){
targetFile.mkdirs();
}
String targetName = resourcesFile.getName()+”.zip”; //目的壓縮文件名
FileOutputStream outputStream = new FileOutputStream(targetPath+”\\”+targetName);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));
createCompressedFile(out, resourcesFile, “”);
out.close();
}
/**
* @desc 生成壓縮文件。
* 如果是文件夾,則使用遞歸,進行文件遍歷、壓縮
* 如果是文件,直接壓縮
* @param out 輸出流
* @param file 目標文件
* @return void
* @throws Exception
*/
public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
//如果當前的是文件夾,則進行進一步處理
if(file.isDirectory()){
//得到文件列表信息
File[] files = file.listFiles();
//將文件夾添加到下一級打包目錄
out.putNextEntry(new ZipEntry(dir+”/”));
dir = dir.length() == 0 ? “” : dir +”/”;
//循環將文件夾中的文件打包
for(int i = 0 ; i files.length ; i++){
createCompressedFile(out, files[i], dir + files[i].getName()); //遞歸處理
}
}
else{ //當前的是文件,打包處理
//文件輸入流
FileInputStream fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
//進行寫操作
int j = 0;
byte[] buffer = new byte[1024];
while((j = fis.read(buffer)) 0){
out.write(buffer,0,j);
}
//關閉輸入流
fis.close();
}
}
public static void main(String[] args){
CompressedFileUtil compressedFileUtil = new CompressedFileUtil();
try {
compressedFileUtil.compressedFile(“G:\\zip”, “F:\\zip”);
System.out.println(“壓縮文件已經生成…”);
} catch (Exception e) {
System.out.println(“壓縮文件生成失敗…”);
e.printStackTrace();
}
}
}
運行程序結果如下:
壓縮之前的文件目錄結構:
如果是使用java.util下的java.util.zip進行打包處理,可能會出現中文亂碼問題,這是因為java的zip方法不支持編碼格式的更改,我們可以使用ant.java下的zip工具類來進行打包處理。所以需要將ant.jar導入項目的lib目錄下。
如何使用JAVA代碼壓縮PDF文件
用java代碼壓縮應用到程序了,代碼一般是比較複雜的,對pdf文件的mate標籤優化,這類標籤包括三類,pdf文件不是網頁就是個文件,何況我們可以用pdf壓縮工具壓縮,下面有個解決方法,樓主可以做參照。
1:點擊打開工具,打開主頁面上有三個功能進行選擇,我們選擇pdf文件壓縮。
2:這這個頁面中我們選擇pdf文件在這裡打開,點擊「添加文件」按鈕將文件添加進來。
3:然後在頁面中點擊「開始壓縮」就可以開始壓縮文件了。
4:壓縮完成的文件頁面會顯示已經完成。
如何使用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(“文件打包失敗!”);
}
}
}
java中的壓縮原理是什麼?
什麼是壓縮文件?
簡單的說,就是經過壓縮軟體壓縮的文件叫壓縮文件,壓縮的原理是把文件的二進位代碼壓縮,把相鄰的0,1代碼減少,比如有000000,可以把它變成6個0
的寫法60,來減少該文件的空間。
■怎麼壓縮文件?
首先要安裝壓縮軟體,現在比較流行的是WinRAR「一種高效快速的文件壓縮軟體(中文版)」。
其次是建立一個壓縮包:選擇你要製作成壓縮包的文件或文件夾,當然你也可也多選,方法同資源管理器,也就是按住Ctrl或Shift再選擇文件(文件夾)。
選取完畢之後,就可以單擊工具欄上的「壓縮」按鈕,在這裡你可以選擇壓縮格式:RAR和ZIP。
如果你想得到較大的壓縮率,建議選擇RAR格式。
各個選項選擇好以後,單擊確定按鈕就開始製作壓縮包了,非常方便。有時候大家會遇到這個問題,就是你在一個論壇里要上傳一些文件壓縮包,壓縮包大小有3M,但是論壇限制會員上傳大小只有2M,怎麼辦呢?
其實辦法很簡單,就是在你壓縮這個文件時,分成幾個帶分卷壓縮包,分卷包大小設置為2M即可,比如:原來文件名為123.rar(3M),壓縮成分卷包後為123.part1.rar(2M)與123.part2.rar(1M)兩個文件,這樣你就可以上傳了。
具體方法如下:
1、在要壓縮的文件上點右鍵
2、添加到壓縮文件….
3、選常規
4、壓縮方式選最好
5、批定壓縮分卷大小(按位元組計算),1M
=
1024K,1K
=
1024位元組,填寫數字即可
當你下載了帶有分卷的壓縮包後,如何解壓文件呢?
具體方法如下:
1、把所有的壓縮分卷全部下載完整
2、所有分卷必須在同一個文件夾內
3、然後雙擊解壓第一個分卷,即可
註:分卷解壓的文件必須是連續的,若分卷未下載完整,則解壓時自然會提示需要下一壓縮分卷
如何使用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包
在JDK中有一個zip工具類:blockquotejava.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats./blockquote使用此類可以將文件夾或者多個文件進行打包壓縮操作。在使用之前先了解關鍵方法:blockquoteZipEntry(String name) Creates a new zip entry with the specified name./blockquote使用ZipEntry的構造方法可以創建一個zip壓縮文件包的實例,然後通過ZipOutputStream將待壓縮的文件以流的形式寫進該壓縮包中。具體實現代碼如下:pre t=”code” l=”java”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 = “file”;
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
if(flag){
System.out.println(“文件打包成功!”);
}else{
System.out.println(“文件打包失敗!”);
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/301342.html