谁有把多个文件夹压缩成zip文件的java方法分享一个
发布网友
发布时间:2022-04-22 13:02
我来回答
共4个回答
热心网友
时间:2022-04-27 16:40
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("d:\\test.zip"));
String test1="test1";
String test2="test2";
byte[] bytes1 = test1.getBytes("UTF-8");
byte[] bytes2 = test2.getBytes("UTF-8");
ZipEntry z1 = new ZipEntry("test1.txt");
zos.putNextEntry(z1);
zos.write(bytes1);
ZipEntry z2 = new ZipEntry("text2.txt");
zos.putNextEntry(z2);
zos.write(bytes2);
zos.closeEntry();
zos.close();
//流可以自己获取
//java默认的包不支持中文(乱码)
//使用apache的ZipOutputStream进行zip压缩
热心网友
时间:2022-04-27 17:58
用Ant吧在配置文件中写成需要导出的文件格式就OK追问我想要个具体的实例。。。
热心网友
时间:2022-04-27 19:33
import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZipFSPUser {
public static void main(String [] args) throws Throwable {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
// locate file system by using the syntax
// defined in java.net.JarURLConnection
URI uri = URI.create("jar:file:/codeSamples/zipfs/zipfstest.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path externalTxtFile = Paths.get("/codeSamples/zipfs/SomeTextFile.txt");
Path pathInZipfile = zipfs.getPath("/SomeTextFile.txt");
// copy a file into the zip file
externalTxtFile.copyTo(pathInZipfile);
}
}
}
jdk7
热心网友
时间:2022-04-27 21:24
java 的流操作中有相关方法和类