import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class MenuTest {
private String sourceFilePath;
private String targetFilePath;
public String getSourceFilePath() {
return sourceFilePath;
}
public void setSourceFilePath(String sourceFilePath) {
this.sourceFilePath = sourceFilePath;
}
public String getTargetFilePath() {
return targetFilePath;
}
public void setTargetFilePath(String targetFilePath) {
this.targetFilePath = targetFilePath;
}
public String getFilePath(String message) {
String filePath;
System.out.println(message);
Scanner scanner = new Scanner(System.in);
filePath = scanner.next();
filePath.replace("\\", File.separator);
return filePath;
}
public void fileCopy () throws IOException{
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File sourceFile = new File(getSourceFilePath());
File targetFile = new File(getTargetFilePath());
if (!sourceFile.exists()) {
System.out.println("souce file not exists.");
System.exit(1);
}
InputStream input = new FileInputStream(sourceFile);
OutputStream output = new FileOutputStream(targetFile);
if((input!=null)&&(output!=null)){
int temp=0;
while((temp=input.read())!=(-1)){
output.write(temp);
}
}
input.close();
output.close();
}
public void zipDecompress() throws IOException {
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File souceFile = new File(getSourceFilePath());
File outFile = null;
ZipFile zipFile = new ZipFile(souceFile);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(
souceFile));
ZipEntry entry = null;
InputStream input = null;
OutputStream output = null;
while ( (entry = zipInput.getNextEntry()) != null) {
System.out.println("解压缩" + entry.getName() + "文件");
outFile = new File(getTargetFilePath());
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdir();
}
if(!outFile.exists()){
outFile.createNewFile();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(outFile);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
}
public void zipCompress() throws IOException {
String message = "Please input source file path:";
this.setSourceFilePath(getFilePath(message));
message = "Please input target file path:";
this.setTargetFilePath(getFilePath(message));
File sourceFile = new File(getSourceFilePath());
File zipFile = new File(getTargetFilePath());
InputStream input = new FileInputStream(sourceFile);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
zipOut.close();
}
public static void main(String[] args) {
MenuTest menuTest = new MenuTest();
Integer menu = 0;
System.out.println("Please input a number to select a menu:");
System.out.println("1.Copy File 2.Comress File 3.Decomress File 4.quit");
Scanner scanner = new Scanner(System.in);
menu = scanner.nextInt();
try {
switch (menu) {
case 1:
menuTest.fileCopy();
break;
case 2:
menuTest.zipCompress();
break;
case 3:
menuTest.zipDecompress();
break;
default :
System.exit(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章
- 10-05解决方案看起来是受源代码管理,但无法找到它的绑定信息。保存解决方案的源代码管理设置的MSSCCPRJ.SCC文件或其他项可能己被删除。
- 10-05标准IO和文件IO区别
- 10-05安卓文件管理器
- 10-05IO编程(1)-文件读写
- 10-05用户和组配置及文件管理命令
- 10-05从文件协同到内容驱动业务:不断进化的企业内容管理需求与随遇而安的产品滞后
- 10-05文件管理基础命令之一
- 10-05文件夹管理
- 10-05文件管理基础命令之一
- 10-05os.path:平*立的文件名管理