java中解压⽂件
1、maven 中引⼊ apache 解压 / 压缩⼯具类
<!-- mvnrepository/artifact/org.apachemons/commons-compress -->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
2、代码中使⽤
(1)将 转换为 tar
(2)解压 tar
import org.apachemonspress.archivers.tar.TarArchiveEntry;
import org.apachemonspress.archivers.tar.TarArchiveInputStream;
import ip.GzipCompressorInputStream;
import org.apachemonspress.utils.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
l.sax.EntityResolver;
l.sax.InputSource;
l.sax.SAXException;
// ⽂件路径
String sourcePath = "D:\\";
// 要解压到的⽬录
使用dom4j解析xml文件String extractPath = "D:\\test\\daleyzou";
File sourceFile = new File(sourcePath);
// decompressing *. files to tar
TarArchiveInputStream fin = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(sourceFile))); File extraceFolder = new File(extractPath);
TarArchiveEntry entry;
// 将 tar ⽂件解压到 extractPath ⽬录下
while ((entry = NextTarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
File curfile = new File(extraceFolder, Name());
File parent = ParentFile();
if (!ists()) {
parent.mkdirs();
}
// 将⽂件写出到解压的⽬录
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论