java写zip后无法读取

  1. 使用IO写ZIP之后导致ZIP文件无法读取

获取到ZIP文件的InputStream之后通过DataOutoutStream写入到文件,写完之后再使用ZipFile读取时报无法打开zip文件

实现代码:

public static String writeZip(String path,String fileName,InputStream in) {

String str = “”;

if(StringUtils.isEmpty(path))

throw new NullPointerException(“file path is null.”);

if(StringUtils.isEmpty(fileName))

throw new NullPointerException(“file name is null.”);

String filePath = path + File.separator + fileName;

DataOutputStream out = null;

try {

out = new DataOutputStream(new FileOutputStream(filePath));

byte [] b = new byte[1024];

int readCount = in.read(b);

while(readCount > -1) {

out.write(b);

readCount = in.read(b);

}

str = new String(b,“UTF-8”);

logger.info("ca receive data : " + str);

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(out != null) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return str;

}

上面文件的写入方式存在一个bug,当文件大小不是1024的整数倍时会导致数组中多余的null字节也会写入文件,最终导致文件原始内容被破坏导致无法读取zip文件

解决方法:

public static String writeZip(String path,String fileName,InputStream in) {

String str = “”;

if(StringUtils.isEmpty(path))

throw new NullPointerException(“file path is null.”);

if(StringUtils.isEmpty(fileName))

throw new NullPointerException(“file name is null.”);

String filePath = path + File.separator + fileName;

DataOutputStream out = null;

try {

out = new DataOutputStream(new FileOutputStream(filePath));

byte [] b = new byte[1024];

int readCount = in.read(b);

while(readCount > -1) {

if(readCount < 1024) {

byte [] temp = new byte[readCount];

System.arraycopy(b, 0, temp, 0, readCount);

out.write(temp);

}else {

out.write(b);

}

readCount = in.read(b);

}

str = new String(b,“UTF-8”);

logger.info("ca receive data : " + str);

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

if(out != null) {

try {

out.close();

} catch (IOE​​​

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Javajava.util.zip包来实现解压zip文件并读取其内容。下面是一个示例代码: ```java import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipExample { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/file.zip"; String destinationFolder = "path/to/destination/folder"; try { unzip(zipFilePath, destinationFolder); readContents(destinationFolder); } catch (IOException e) { e.printStackTrace(); } } public static void unzip(String zipFilePath, String destinationFolder) throws IOException { byte[] buffer = new byte[1024]; File folder = new File(destinationFolder); if (!folder.exists()) { folder.mkdirs(); } try (ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFilePath)))) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.getNextEntry()) != null) { String entryName = zipEntry.getName(); File entryFile = new File(destinationFolder + File.separator + entryName); if (entryFile.getParentFile() != null && !entryFile.getParentFile().exists()) { entryFile.getParentFile().mkdirs(); } if (!zipEntry.isDirectory()) { int bytesRead; while ((bytesRead = zipInputStream.read(buffer)) != -1) { entryFile.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(entryFile); fileOutputStream.write(buffer, 0, bytesRead); fileOutputStream.close(); } } else { entryFile.mkdirs(); } zipInputStream.closeEntry(); } } } public static void readContents(String folderPath) { File folder = new File(folderPath); if (folder.exists() && folder.isDirectory()) { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isDirectory()) { System.out.println("Directory: " + file.getName()); } else { System.out.println("File: " + file.getName()); } } } } } } ``` 在上述示例代码中,你需要替换`zipFilePath`和`destinationFolder`变量的值为你实际的zip文件路径和解压后的目标文件夹路径。`unzip`方法负责解压zip文件,`readContents`方法用于读取解压后的文件夹内容并打印。 请确保你已经正确配置了Java环境,并导入了必要的类和包。运行示例代码后,它将解压zip文件并打印出解压后的文件和文件夹列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值