Last active
March 2, 2016 18:17
-
-
Save AnnaBoro/eabc2b5ec3c7b3d93100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson8.chars; | |
import java.io.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
import java.util.zip.ZipOutputStream; | |
public class DemoZip { | |
private String command; | |
private String path; | |
private List<File> files = new ArrayList<File>(); | |
private File file; | |
public DemoZip(String command, String path) { | |
this.command = command; | |
this.path = path; | |
file = new File(path); | |
} | |
public static void main(String[] args) { | |
String command = args[0]; | |
String path = args[1]; | |
DemoZip demoZip = new DemoZip(command, path); | |
demoZip.start(); | |
} | |
public void start() { | |
if (command.equalsIgnoreCase("zip")) { | |
zip(); | |
} | |
else if (command.equalsIgnoreCase("unzip")) { | |
unZip(); | |
} | |
else { | |
System.out.println("Unknown command"); | |
System.exit(0); | |
} | |
} | |
public void zip() { | |
try (FileOutputStream fos = new FileOutputStream(file.getName() + ".zip"); | |
ZipOutputStream zos = new ZipOutputStream(fos);){ | |
files = getFolderTree(file); | |
for (File f : files) { | |
if(f.isFile()) { | |
ZipEntry ze = new ZipEntry(f.getAbsoluteFile().toString()); | |
zos.putNextEntry(ze); | |
FileInputStream in = new FileInputStream(f); | |
int len; | |
byte[] buffer = new byte[1024]; | |
while ((len = in.read(buffer)) > 0) { | |
zos.write(buffer, 0, len); | |
} | |
in.close(); | |
} | |
else if (f.isDirectory()) { | |
getFolderTree(f); | |
} | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void unZip() { | |
byte[] buffer = new byte[1024]; | |
try{ | |
File folder = new File("unzip" + file.getName().substring(0, file.getName().indexOf("."))); | |
System.out.println(file.getName().substring(0, file.getName().indexOf("."))); | |
if(!folder.exists()){ | |
folder.mkdir(); | |
} | |
ZipInputStream zis = new ZipInputStream(new FileInputStream(path)); | |
System.out.println("path______ "+ path); | |
ZipEntry ze = zis.getNextEntry(); | |
while(ze!=null){ | |
String fileName = ze.getName(); | |
File newFile = new File(folder + File.separator + fileName); | |
System.out.println("file unzip : "+ newFile.getAbsoluteFile()); | |
new File(newFile.getParent()).mkdirs(); | |
FileOutputStream fos = new FileOutputStream(newFile); | |
int len; | |
while ((len = zis.read(buffer)) > 0) { | |
fos.write(buffer, 0, len); | |
} | |
fos.close(); | |
ze = zis.getNextEntry(); | |
} | |
zis.closeEntry(); | |
zis.close(); | |
System.out.println("Done"); | |
}catch(IOException ex){ | |
ex.printStackTrace(); | |
} | |
} | |
public List<File> getFolderTree(File file) { | |
for (File f : file.listFiles()) { | |
if (f.isFile()) { | |
files.add(new File(f.getAbsoluteFile().toString())); | |
} | |
else if (f.isDirectory()) { | |
System.out.print("\t"); | |
getFolderTree(f); | |
} | |
} | |
return files; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment