Java后台实现浏览器一键导出下载后台实现浏览器一键导出下载zip压缩包压缩包
主要为大家详细介绍了Java后台实现浏览器一键导出下载zip压缩包,具有一定的参考价值,感兴趣的小伙伴们可
以参考一下
使用迭代器模式和组合模式实现浏览器一键导出下载为zip压缩包文件
由于项目需要,于是又想起之前看过的设计模式,于是便有了一键导出的想法。
思路简单明了。一步一步看下去就好。
1.创建组合对象创建组合对象
public abstract class FileComponent {
/**
* Description: 递归创建文件夹,或者文件
*/
public void mkFile(){
throw new UnsupportedOperationException();
}
/**
* Description: 获取文件输入路径
*/
public String getInPath(){
throw new UnsupportedOperationException();
}
/**
* Description: 获取文件输出路径
*/
public String getOutPath(){
throw new UnsupportedOperationException();
}
/**
* Description: 对于文件夹来说是可以add其他文件夹或者文件
*/
public void add(FileComponent fileComponent){
throw new UnsupportedOperationException();
}
}
此组合对象,可以是文件夹对象,也可是具体的文件对象,再后面调用中,不需要了解到底是一个文件夹还是一个文件(即组合
模式的透明性)。
2.组合对象抽象类的实现组合对象抽象类的实现
上述抽象类的实现如下:
public class ZipFileItem extends FileComponent{
//输入文件的路径
String inPath;
//输出文件的路径
String outPath;
//子节点文件信息
List<FileComponent> fileComponents = new ArrayList<FileComponent>();
//inPath 可以为null
public ZipFileItem(String outPath){
this.outPath =outPath;
}
//压缩文件的源目录路径和压缩好的目标位置
public ZipFileItem(String inPath,String outPath){
this.inPath =inPath;
this.outPath =outPath;
}
public void add(FileComponent fileComponent){
fileComponents.add(fileComponent);
}
public void remove(FileComponent fileComponent){
fileComponents.remove(fileComponent);
}
@Override
public String getInPath(){
return inPath;
}
@Override
public String getOutPath(){
return outPath;
}
评论0