我想制作一个win zip软件。我正在阅读java stream classes
和zip class
,但我的想法无法在这个主题中发挥作用。请帮帮我怎么做这是我在大学里的一个小项目。我在java中创建了两个项目。
但老师说管理项目不允许。请帮帮我
此代码在一个目录中用于多目录
我的程序代码
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.*;
import java.io.*;
class MyZip{
FileInputStream fis;
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;
ZipOutputStream zout;
ZipEntry ze;
public MyZip()throws IOException{
Console con=System.console();
System.out.println("How many directories do u want to compressed:");
int no_file=Integer.parseInt(con.readLine());
String inputFile[]=new String[no_file];
File fileArray[]=new File[no_file];
System.out.println("Enter the path of directories to be compressed:");
for(int i=0;i<inputFile.length;i++){
inputFile[i]=con.readLine();
fileArray[i]=new File(inputFile[i]);
}
String outputFile="E:\\MyProgram\\MyZip\\MyZip.zip";
fos=new FileOutputStream(outputFile);
bos=new BufferedOutputStream(fos);
zout=new ZipOutputStream(bos);
zipFile(fileArray);
zout.close();
bos.close();
fos.close();
getZipEntry();
}
public void getZipEntry()throws IOException{
ZipFile zf=new ZipFile("E:\\MyProgram\\MyZip\\MyZip.zip");
System.out.println(zf.getName());//return name of zip file
Enumeration e=zf.entries();
while(e.hasMoreElements()){
ZipEntry ze=(ZipEntry)e.nextElement();
System.out.print(ze.getName()+"\t");//return name of entry
System.out.print(ze.getSize()+"\t");//return uncompressed size
System.out.print(ze.getCompressedSize());//return compressed size
System.out.println();
}
zf.close();
}
public void zipFile(File farr[])throws IOException{
for(File f:farr){
if(f.isFile()){
writeFile(f);
}
if(f.isDirectory()){
File fileArray[]=f.listFiles();
zipFile(fileArray);
}
}
}
public void writeFile(File f)throws IOException{
ze=new ZipEntry(f.getPath());
zout.putNextEntry(ze);
fis=new FileInputStream(f);
bis=new BufferedInputStream(fis);
int ch;
while((ch=bis.read())!=-1)
zout.write(ch);
bis.close();
fis.close();
zout.closeEntry();
zout.flush();
}
public static void main(String args[]) throws IOException{
new MyZip();
}
}
答案 0 :(得分:0)
仅用于zip解压缩代码
import java.io.*;
import java.util.*;
import java.util.zip.*;
class MyUnZip
{
FileInputStream fis;
BufferedInputStream bis;
ZipInputStream zis;
FileOutputStream fos;
BufferedOutputStream bos;
public MyUnZip(String inputFile)throws IOException{
String path1=inputFile.substring(0,inputFile.lastIndexOf('.'));
//System.out.println(path1);
fis=new FileInputStream(inputFile);
bis=new BufferedInputStream(fis);
zis=new ZipInputStream(bis);
File outputFile;
ZipEntry ze=null;
while((ze=zis.getNextEntry())!=null){
String str=ze.getName();
//System.out.println(str);
File fileName=new File(str);
String path2=str.substring(str.indexOf('\\'),str.lastIndexOf('\\'));
//System.out.println(path2);
String path3=path1+path2;
System.out.println(path3);
File filePath=new File(path3);
filePath.mkdirs();
outputFile=new File(path3,fileName.getName());
fos=new FileOutputStream(outputFile);
bos=new BufferedOutputStream(fos);
int ch;
while((ch=zis.read())!=-1)
bos.write(ch);
bos.close();
fos.close();
zis.closeEntry();
}
zis.close();
}
public static void main(String args[]) throws IOException{
Console con=System.console();
System.out.println("Enter the path of zip file to be uncompressed:");
String inputFile=con.readLine();
new MyUnZip(inputFile);
}
}