目录
6、实体类FileMessage,我这里只记录了文件名和文件大小
1、导入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>3.0.0</version>
</dependency>
2、获取zip压缩文件的文件列表(不包含文件夹)
public List<FileMessage> getZipFileList(String zipFileName) throws IOException {
ZipInputStream zipInputStream = null;
try (FileInputStream inputStream = new FileInputStream(zipFileName)) {
zipInputStream = new ZipInputStream(
new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
java.util.zip.ZipEntry ze;
List<FileMessage> list = new ArrayList<>();
//循环遍历
while ((ze = zipInputStream.getNextEntry()) != null) {
if (!ze.isDirectory()) {
list.add(new FileMessage(ze.getName(), ze.getSize()));
}
}
return list;
} catch (Exception e) {
logger.error(" getZipOneFileContent error:" + e.getMessage());
return null;
} finally {
if (zipInputStream != null) {
zipInputStream.closeEntry();
}
}
}
3、获取zip文件的其中一个文本的内容
public String getZipOneTestFileContent(String zipFileName, String fileName) throws IOException {
ZipInputStream zipInputStream = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(zipFileName);
zipInputStream = new ZipInputStream(
new BufferedInputStream(inputStream), StandardCharsets.UTF_8);
java.util.zip.ZipEntry ze;
//循环遍历
StringBuilder txtStr = new StringBuilder();
while ((ze = zipInputStream.getNextEntry()) != null) {
if (!ze.isDirectory() && ze.getName().equals(fileName)) {
byte[] b = new byte[1024];
int leng;
while ((leng = zipInputStream.read(b)) != -1) {
txtStr.append(new String(b, 0, leng));
}
break;
}
}
return txtStr.toString();
} catch (Exception e) {
logger.error(" getZipOneFileContent error:" + e.getMessage());
return null;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (zipInputStream != null) {
zipInputStream.close();
}
}
}
4、获取rar文件的文件列表(不包括文件夹)
注意rar的版本不能过高,如图所示选择rar4
public List<FileMessage> getRarList(String rarFileName) throws IOException {
FileInputStream inputStream = null;
Archive archive = null;
try {
inputStream = new FileInputStream(rarFileName);
archive = new Archive(inputStream);
FileHeader fileHeader = archive.nextFileHeader();
List<FileMessage> list = new ArrayList<>();
while (fileHeader != null) {
if (fileHeader.isDirectory()) {
continue;
}
list.add(new FileMessage(fileHeader.getFileNameString(), fileHeader.getFullUnpackSize()));
fileHeader = archive.nextFileHeader();
}
return list;
} catch (Exception e) {
logger.error(" getRarFileList error:" + e.getMessage());
return null;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (archive != null) {
archive.close();
}
}
}
5、获取rar文件其中一个文本文件的文件内容
public String getRarContent(String rarFileName, String fileName) throws IOException {
FileInputStream inputStream = null;
Archive archive = null;
InputStream fileInputStream = null;
try {
inputStream = new FileInputStream(rarFileName);
archive = new Archive(inputStream);
FileHeader fileHeader = archive.nextFileHeader();
StringBuilder txtStr = new StringBuilder();
while (fileHeader != null) {
if (fileName.equals(fileHeader.getFileNameString())) {
byte[] b = new byte[1024];
int len;
fileInputStream = archive.getInputStream(fileHeader);
while ((len = fileInputStream.read(b)) != -1) {
txtStr.append(new String(b, 0, len));
}
break;
}
fileHeader = archive.nextFileHeader();
}
return txtStr.toString();
} catch (Exception e) {
logger.error(" getRarFileContent error:" + e.getMessage());
return null;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (archive != null) {
archive.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
6、实体类FileMessage,我这里只记录了文件名和文件大小
public class FileMessage {
private String fileName;
private Long fileSize;
public FileMessage(String fileName, Long fileSize) {
this.fileName = fileName;
this.fileSize = fileSize;
}
}