如何使用java压缩文件夹本身 [英] how to zip a folder itself using java
问题描述
D:\reports\january\
在一月份里有两个excel文件说A.xls和B.xls。有很多地方已经写了关于如何使用 java.util.zip
压缩文件。但是我想将janages文件夹本身压缩在报告文件夹中,以便报告中会出现一月份和 january.zip 。 ( 这意味着当我解压缩january.zip文件时,我应该获得一月份文件夹 )。
任何人都可以使用 java.util.zip
为我提供代码。请让我知道这可以通过使用其他库更容易做到。
非常感谢...
解决包 java.util.Zip
不需要任何额外的 Jar
文件
只需复制以下代码,并将与
/ p> IDE
一起运行
//导入所有需要的包
package general;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private List< String>的fileList;
private static final String OUTPUT_ZIP_FILE =Folder.zip;
private static final String SOURCE_FOLDER =D:\\Reports; // SourceFolder path
public ZipUtils(){
fileList = new ArrayList<字符串> ();
}
public static void main(String [] args){
ZipUtils appZip = new ZipUtils();
appZip.generateFileList(new File(SOURCE_FOLDER));
appZip.zipIt(OUTPUT_ZIP_FILE);
}
public void zipIt(String zipFile){
byte [] buffer = new byte [1024];
String source = new File(SOURCE_FOLDER).getName();
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
System.out.println(Output to Zip:+ zipFile);
FileInputStream in = null;
(String file:this.fileList){
System.out.println(File Added:+ file);
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
尝试{
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while((len = in .read(buffer))> 0){
zos.write(buffer,0,len);
}
} finally {
in.close();
}
}
zos.closeEntry();
System.out.println(Folder successfully compressed);
} catch(IOException ex){
ex.printStackTrace();
} finally {
try {
zos.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
public void generateFileList(File node){
//仅添加文件
if(node.isFile )){
fileList.add(generateZipEntry(node.toString()));
}
if(node.isDirectory()){
String [] subNote = node.list();
for(String filename:subNote){
generateFileList(new File(node,filename));
}
}
}
private String generateZipEntry(String file){
return file.substring(SOURCE_FOLDER.length()+ 1,file。长度());
}
}
参考 mkyong ..i改变了当前问题要求的代码
Suppose I have the following directory structure.
D:\reports\january\
Inside january there are suppose two excel files say A.xls and B.xls. There are many places where it has been written about how to zip files using java.util.zip
. But I want to zip the january folder itself inside reports folder so that both january and january.zip will be present inside reports. (That means when I unzip the january.zip file I should get the january folder).
Can anyone please provide me the code to do this using java.util.zip
. Please let me know whether this can be more easily done by using other libraries.
Thanks a lot...
It can be easily solved by package java.util.Zip
no need any extra Jar
files
Just copy the following code and run it
with your IDE
//Import all needed packages
package general;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private List <String> fileList;
private static final String OUTPUT_ZIP_FILE = "Folder.zip";
private static final String SOURCE_FOLDER = "D:\\Reports"; // SourceFolder path
public ZipUtils() {
fileList = new ArrayList < String > ();
}
public static void main(String[] args) {
ZipUtils appZip = new ZipUtils();
appZip.generateFileList(new File(SOURCE_FOLDER));
appZip.zipIt(OUTPUT_ZIP_FILE);
}
public void zipIt(String zipFile) {
byte[] buffer = new byte[1024];
String source = new File(SOURCE_FOLDER).getName();
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + zipFile);
FileInputStream in = null;
for (String file: this.fileList) {
System.out.println("File Added : " + file);
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
try {
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in .read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
} finally {
in.close();
}
}
zos.closeEntry();
System.out.println("Folder successfully compressed");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void generateFileList(File node) {
// add file only
if (node.isFile()) {
fileList.add(generateZipEntry(node.toString()));
}
if (node.isDirectory()) {
String[] subNote = node.list();
for (String filename: subNote) {
generateFileList(new File(node, filename));
}
}
}
private String generateZipEntry(String file) {
return file.substring(SOURCE_FOLDER.length() + 1, file.length());
}
}
Refer mkyong..i changed the code for the requirement of current question
这篇关于如何使用java压缩文件夹本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!