java 按指定文件夹下的文件的前14个字符串批量打包成zip的代码
时间: 2023-06-03 10:02:13 浏览: 89
java 文件夹打包 zip
以下是可以回答的代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFiles {
public static void main(String[] args) {
String folderPath = "指定文件夹路径";
String zipFilePath = "生成的zip文件路径";
try {
FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
File folder = new File(folderPath);
for (File file : folder.listFiles()) {
if (file.isFile() && file.getName().length() >= 14) {
String fileName = file.getName();
String prefix = fileName.substring(0, 14);
ZipEntry ze = new ZipEntry(prefix + ".txt");
zos.putNextEntry(ze);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
fis.close();
}
}
zos.closeEntry();
zos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文