我只想在特定文件夹中找到7Z文件(仅限.pdf和.txt文件)的代码并将其放在同一个文件夹中。
我可以提供的输入是文件夹位置。
有人可以帮忙解决这个问题吗?
可以用Java或JavaScript完成吗?
我是初学者。需要帮助..提前谢谢
答案 0 :(得分:0)
我不确切地知道你想要什么,但以下内容可能有所帮助:
/*
* Search for files in the folder and copy
* them into a zip file.
*/
//Your files
File folder = new File("C:\\Java\\Example\\");
File destination = new File("C:\\Java\\ZipFile.zip");
//Output streams for writing the zip file
FileOutputStream fos = new FileOutputStream(destination);
ZipOutputStream zos = new ZipOutputStream(fos);
//Search for files in the directory, ignore sub-directories
for(File file : folder.listFiles()) {
if(file.isDirectoy())
continue;
//Create a zip entry and set the ZipOutputStream to
//write a new file inside the zip file.
ZipEntry entry = new ZipEntry(file.getName());
zos.putNextEntry(entry);
//Open a FileInputStream to read from the current file
FileInputStream fis = new FileInputStream(file);
//Stream the contents of the file directly
//to the ZipOutputStream
int length;
byte[] buffer = new byte[1024];
while((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
//Close the input stream and the zip
//entry for the next file
fis.close();
zos.closeEntry();
}
//Close the ZipOutputStream
zos.close();
如果您有一个名为Example
的文件夹,则所有文件都将被复制到一个zip文件中。