Java 使用util.zip No目录压缩文件
我有以下情况: 我可以使用以下方法压缩我的文件:Java 使用util.zip No目录压缩文件,java,zip,Java,Zip,我有以下情况: 我可以使用以下方法压缩我的文件: public boolean generateZip(){ byte[] application = new byte[100000]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); // These are the files to include in the ZIP file String[] filenames = new String[
public boolean generateZip(){
byte[] application = new byte[100000];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// These are the files to include in the ZIP file
String[] filenames = new String[]{"/subdirectory/index.html", "/subdirectory/webindex.html"};
// Create a buffer for reading the files
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(baos);
// Compress the files
for (int i=0; i<filenames.length; i++) {
byte[] filedata = VirtualFile.fromRelativePath(filenames[i]).content();
ByteArrayInputStream in = new ByteArrayInputStream(filedata);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(application)) > 0) {
out.write(application, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
System.out.println("There was an error generating ZIP.");
e.printStackTrace();
}
downloadzip(baos.toByteArray());
}
public boolean generateZip(){
字节[]应用程序=新字节[100000];
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
//这些是要包含在ZIP文件中的文件
字符串[]文件名=新字符串[]{“/subdirectory/index.html”,“/subdirectory/webindex.html”};
//创建用于读取文件的缓冲区
试一试{
//创建ZIP文件
ZipOutputStream out=新的ZipOutputStream(BAS);
//压缩文件
对于(int i=0;i 0){
out.write(应用程序,0,len);
}
//完成条目
out.closeEntry();
in.close();
}
//完成ZIP文件
out.close();
}捕获(IOE异常){
System.out.println(“生成ZIP时出错”);
e、 printStackTrace();
}
下载zip(baos.toByteArray());
}
这非常有效,我可以下载xy.zip,其中包含以下目录和文件结构:子目录/
----index.html
----webindex.html
我的目标是完全省略子目录,zip应该只包含这两个文件。有没有办法做到这一点? (我在谷歌应用程序引擎上使用Java)
提前感谢如果您确定
文件名
数组中包含的文件是唯一的,如果您省略了目录,请更改构建ZipEntry的行
s:
String zipEntryName = new File(filenames[i]).getName();
out.putNextEntry(new ZipEntry(zipEntryName));
这将使用如果您确定
文件名
数组中包含的文件是唯一的,如果您省略目录,请更改用于构造ZipEntry的行
s:
String zipEntryName = new File(filenames[i]).getName();
out.putNextEntry(new ZipEntry(zipEntryName));
这使用了您可以使用
虚拟文件上的isDirectory()
方法您可以使用虚拟文件上的isDirectory()
方法列出所有文件,然后将它们读取到输入流中
替换下面的行
String[] filenames = new String[]{"/subdirectory/index.html", "/subdirectory/webindex.html"}
有以下几点
Collection<File> files = FileUtils.listFiles(new File("/subdirectory"), new String[]{"html"}, true);
for (File file : files)
{
FileInputStream fileStream = new FileInputStream(file);
byte[] filedata = IOUtils.toByteArray(fileStream);
//From here you can proceed with your zipping.
}
Collection files=FileUtils.listFiles(新文件(“/subdirectory”),新字符串[]{“html”},true);
用于(文件:文件)
{
FileInputStream fileStream=新的FileInputStream(文件);
byte[]filedata=IOUtils.toByteArray(fileStream);
//从这里开始,您可以继续您的拉链。
}
如果您有问题,请告诉我。您可以使用列出所有文件,然后将它们读取到InputStream
替换下面的行
String[] filenames = new String[]{"/subdirectory/index.html", "/subdirectory/webindex.html"}
有以下几点
Collection<File> files = FileUtils.listFiles(new File("/subdirectory"), new String[]{"html"}, true);
for (File file : files)
{
FileInputStream fileStream = new FileInputStream(file);
byte[] filedata = IOUtils.toByteArray(fileStream);
//From here you can proceed with your zipping.
}
Collection files=FileUtils.listFiles(新文件(“/subdirectory”),新字符串[]{“html”},true);
用于(文件:文件)
{
FileInputStream fileStream=新的FileInputStream(文件);
byte[]filedata=IOUtils.toByteArray(fileStream);
//从这里开始,您可以继续您的拉链。
}
如果您有问题,请告诉我。绝妙的解决方案!非常感谢。绝妙的解决方案!非常感谢。