如何在java中压缩多个文件,以便在解压时,它不应位于文件夹中
我想压缩3个文件 我可以用下面的代码压缩这些文件如何在java中压缩多个文件,以便在解压时,它不应位于文件夹中,java,file,zip,unzip,Java,File,Zip,Unzip,我想压缩3个文件 我可以用下面的代码压缩这些文件 public class TestZip { public void zipFiles(List<String> files){ FileOutputStream fos = null; ZipOutputStream zipOut = null; FileInputStream fis = null; try { fos = new FileOutputStream("Neoload
public class TestZip {
public void zipFiles(List<String> files){
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream("NeoloadProject/New folder/testing.zip");
zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
for(String filePath:files){
File input = new File(filePath);
fis = new FileInputStream(input);
ZipEntry ze = new ZipEntry(input.getName());
System.out.println("Zipping the file: "+input.getName());
zipOut.putNextEntry(ze);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = fis.read(tmp)) != -1){
zipOut.write(tmp, 0, size);
}
zipOut.flush();
fis.close();
}
zipOut.close();
System.out.println("Done... Zipped the files...");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(fos != null) fos.close();
} catch(Exception ex){
}
}
}
public static void main(String a[]){
TestZip mfe = new TestZip();
List<String> files = new ArrayList<String>();
files.add("NeoloadProject/New folder/repository.xml");
files.add("NeoloadProject/New folder/scenario.xml");
files.add("NeoloadProject/New folder/settings.xml");
mfe.zipFiles(files);
}
公共类TestZip{
公共无效ZipFile(列表文件){
FileOutputStream=null;
ZipOutputStream zipOut=null;
FileInputStream fis=null;
试一试{
fos=新文件输出流(“NeoloadProject/new folder/testing.zip”);
zipOut=新的ZipOutputStream(新的缓冲输出流(fos));
用于(字符串文件路径:文件){
文件输入=新文件(文件路径);
fis=新文件输入流(输入);
ZipEntry ze=新的ZipEntry(input.getName());
System.out.println(“压缩文件:+input.getName());
putNextEntry(泽);
字节[]tmp=新字节[4*1024];
int size=0;
而((大小=fis.read(tmp))!=-1){
写入(tmp,0,大小);
}
齐平();
fis.close();
}
zipOut.close();
System.out.println(“完成…压缩文件…”);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}最后{
试一试{
如果(fos!=null)fos.close();
}捕获(例外情况除外){
}
}
}
公共静态void main(字符串a[]{
TestZip mfe=newtestzip();
列表文件=新的ArrayList();
add(“NeoloadProject/newfolder/repository.xml”);
add(“NeoloadProject/newfolder/scenario.xml”);
添加(“NeoloadProject/New folder/settings.xml”);
mfe.zipFiles(文件);
}
}
所以程序的输出是testing.zip
所以当我解压时,我的所有文件都在测试文件夹中。
测试文件夹-->文件1、文件2、文件3
我的要求是,当我解压这个压缩文件时,它应该直接压缩,不需要任何文件夹包装。文件包含什么?我想将3个文件(.text)压缩成一个压缩文件。当我解压文件时,它不应该将我的所有文件包装到一个文件夹中例如:testing.zip到testing文件夹,然后将其中的所有文件直接解压到所有文件的外部。我讨厌人们通过忽略它并重复自己来回答问题。你是在Java程序中解压文件,还是通过命令行解压文件,还是GUI?如果是命令行,是什么操作系统?命令行选项可能按顺序排列。如果是GUI,哪一个?通过java程序