for( int i =0 i <大文件夹数i ++){
String name = 生成大文件夹名;
File parent = new File(目录 + “/" + name)
parent.mkdir()
for( int j = 0j <30j ++){
String subName = 生成小文件夹名;
File child = new File(parent, subName)
child.mkdir()
}
}
在JDK中有一个zip工具类:
java.util.zip Provides classes for reading and writing the standard ZIP and
GZIP file formats.
使用此类可以将文件夹或者多个文件进行打包压缩操作。
在使用之前先了解关键方法:
ZipEntry(String name) Creates a new zip entry with the specified name.
使用ZipEntry的构造方法可以创建一个zip压缩文件包的实例,然后通过ZipOutputStream将待压缩的文件以流的形式写进该压缩包中。具体实现代码如下:
import java.io.BufferedInputStreamimport java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
/**
* 将文件夹下面的文件
* 打包成zip压缩文件
*
* @author admin
*
*/
public final class FileToZip {
private FileToZip(){}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param sourceFilePath :待压缩的文件路径
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){
boolean flag = false
File sourceFile = new File(sourceFilePath)
FileInputStream fis = null
BufferedInputStream bis = null
FileOutputStream fos = null
ZipOutputStream zos = null
if(sourceFile.exists() == false){
System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在.")
}else{
try {
File zipFile = new File(zipFilePath + "/" + fileName +".zip")
if(zipFile.exists()){
System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.")
}else{
File[] sourceFiles = sourceFile.listFiles()
if(null == sourceFiles || sourceFiles.length<1){
System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.")
}else{
fos = new FileOutputStream(zipFile)
zos = new ZipOutputStream(new BufferedOutputStream(fos))
byte[] bufs = new byte[1024*10]
for(int i=0i<sourceFiles.lengthi++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName())
zos.putNextEntry(zipEntry)
//读取待压缩的文件并写进压缩包里
fis = new FileInputStream(sourceFiles[i])
bis = new BufferedInputStream(fis, 1024*10)
int read = 0
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read)
}
}
flag = true
}
}
} catch (FileNotFoundException e) {
e.printStackTrace()
throw new RuntimeException(e)
} catch (IOException e) {
e.printStackTrace()
throw new RuntimeException(e)
} finally{
//关闭流
try {
if(null != bis) bis.close()
if(null != zos) zos.close()
} catch (IOException e) {
e.printStackTrace()
throw new RuntimeException(e)
}
}
}
return flag
}
public static void main(String[] args){
String sourceFilePath = "D:\\TestFile"
String zipFilePath = "D:\\tmp"
String fileName = "12700153file"
boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName)
if(flag){
System.out.println("文件打包成功!")
}else{
System.out.println("文件打包失败!")
}
}
}
如果自己的java project中需要引用额外的jar包作为资源文件,那么需要自定义配置文件MANIFEST.MF ,例如:\x0d\x0aManifest-Version: 1.0\x0d\x0aClass-Path: lib\crimson.jar lib\jbcl.jar lib\junit.jar lib\log4j-1.2.13.jar lib\mysql-connector-java-3.1.13-bin.jar\x0d\x0aMain-Class: src.YourMainClassName\x0d\x0a其中的Class-Path就指定了外来jar包的位置。请注意假设我们的项目打包后为project.jar,那么按照上面的定义,应该在 project.jar的同层目录下建立一个lib文件夹,并将相关的jar包放在里面。否则将会出现Exception in thread "main" java.lang.NoClassDefFoundError的错误。\x0d\x0a如果想在所导出的jar包中包含第三方jar包,可以如下操作:\x0d\x0a1.在工程目录下放入第三方jar包\x0d\x0a2.Class-Path: name.jar\x0d\x0a上诉两个步骤就可以成功导出引用了jar包的java工程,并将第三方jar包放在导出的工程中\x0d\x0a注意:如果在Class-Path 中引入比较多的jar包,不要写成一行,不然会报:java.io.IOException : line too long ,需要分多行写,第二行以及下面的第三行、第四行写的时候需要在前面空两格(敲两下空格键)即可\x0d\x0a最后介绍一下使用Eclipse制作jar包的过程吧:\x0d\x0a1. 首先在Eclipse中打开项目, 右键点击项目,选择“Export”;\x0d\x0a2. 选择Java/JAR file,Next;\x0d\x0a3. Select the resources to export中可以选择你想要包含的项目文件夹,一些不必要的文件夹就无需放进去了,免得增大空间;\x0d\x0a这里有几个选项:\x0d\x0a* Export generated class files and resources 表示只导出生成的.class文件和其他资源文件\x0d\x0a* Export all output folders for checked projects 表示导出选中项目的所有文件夹\x0d\x0a* Export java source file and resouces 表示导出的jar包中将包含你的源代码*.java,如果你不想泄漏源代码,那么就不要选这项了\x0d\x0a* Export refactorings for checked projects 把一些重构的信息文件也包含进去\x0d\x0a在Select the export destination中选择导出的jar的路径,Next\x0d\x0a4. 下一页可以选择是否导出那些含有警告warning或者错误errors的*.class文件。一般不用理他,Next\x0d\x0a5. 下一个页面里可以对项目做一些配置。\x0d\x0a* Generate the manifest file是系统帮我们自动生成MANIFEST.MF文件,如果你的项目没有引用其他class-path,那可以选择这一项。\x0d\x0a* Use existing mainfest from workspace。这是可以选择我们自定义的.MF文件,格式如上所写。\x0d\x0a* Seal content。要封装整个jar或者指定的包packet\x0d\x0a* Main class。这里可以选择你的程序入口,将来打包出来的jar就是你这个入口类的执行结果。\x0d\x0a最后Finish 完成