JAVA生成压缩文件



String zipFilePath = "C:/Users/Administrator/Desktop/dd.zip";//生成压缩文件的存放路径及名称
List<String> fileList = new ArrayList<String>();//要压缩的文件集合
fileList.add("C:/Users/Administrator/Desktop/1.doc");
fileList.add("C:/Users/Administrator/Desktop/2.doc");
File file = new File(zipFilePath);
if (file.exists()) {
file.delete();
}
try {
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipFilePath);
ZipOutputStream zos = new ZipOutputStream(fos);
ArrayList<File> files = new ArrayList<File>();
for (String fileName : fileList) {
files.add(new File(fileName));
}
for (int i = 0; i < files.size(); i++) {
FileInputStream fis = new FileInputStream(files.get(i));
zos.putNextEntry(new ZipEntry(files.get(i).getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Apache PDFBox库来生成OFD文件,因为OFD文件本质上是一个包含XML和一些其他文件的ZIP文件。以下是一个示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class OFDGenerator { public static void generateOFD() throws IOException, JAXBException { // 创建OFD文件夹 File ofdFolder = new File("example.ofd"); ofdFolder.mkdirs(); // 创建OFD.xml文件 OFD ofd = new OFD(); JAXBContext jaxbContext = JAXBContext.newInstance(OFD.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(ofd, new File(ofdFolder, "OFD.xml")); // 将OFD.xml和其他文件压缩为OFD文件 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream("example.ofd")); addToZipFile("OFD.xml", ofdFolder.getAbsolutePath(), zipOut); // 添加其他文件 zipOut.close(); } private static void addToZipFile(String fileName, String folderName, ZipOutputStream zipOut) throws IOException { File file = new File(folderName + "/" + fileName); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); FileInputStream fis = new FileInputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); } } ``` 请注意,这只是一个简单的示例,你需要根据你的具体需求来更改代码。此外,你还需要确保所使用的Apache PDFBox库版本支持OFD生成

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值