当前位置:首页 » 文件管理 » java解压targz

java解压targz

发布时间: 2023-05-12 19:31:34

A. java解压tar.gz用到哪些包




//引入jtar-(版本号).jar

publicvoidreadtar()throwsIOException{
StringtarFile="D:/20120725.tar.gz";
StringdestFolder="D:/20120725";

Filess=newFile(tarFile);
TarInputStreamtis=null;
tis=newTarInputStream(newGZIPInputStream(newBufferedInputStream(newFileInputStream(ss))));
TarEntryentry;

try{
while((entry=tis.getNextEntry())!=null){
intcount;
bytedata[]=newbyte[204800];

FileOutputStreamfos=newFileOutputStream(newFile(destFolder+"/"+entry.getName()));
BufferedOutputStreamdest=newBufferedOutputStream(fos);

while((count=tis.read(data))!=-1){
dest.write(data,0,count);
}
dest.flush();
dest.close();
}
tis.close();

}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}

B. Java 怎么解压zlib函数库压缩的tar.gz格式的文件

这个文件不是在windows环境下解压的,它是在linux系统下的压缩文件。你应该解压zip格式的压缩包

C. java里怎么解压tar.gz文件啊,网上好多例子都不行

最后怎么解决的,我现在也遇到这个问题了,单个文件可以解压可以压缩,写入的测试内容也在,换成文件夹就不行了。能找到的案例全都是解压成文件,但是本身是个文件夹的GZ包解压了以后也打不开。

D. 新人求助,JDK压缩包解压不了

一、下载首先,下载jdkforubuntu。按照需要选择不同的版本。这里选择的是jdk-7u45。jdk-7u45-linux-x64.tar.gz
二、解压将下载下来的.tar.gz文件解压。使用如下命令解压:sudotarzxvf./jdk-7u45-linux-x64.tar.gz
三、设置环境变量编辑.bashrc文件。在终端输入如下命令:vi~/.bashrc在该文件的末尾,加上以上几行代码:exportJAVA_HOME=/opt/Java/jdk/jdk1.7exportCLASSPATH=${JAVA_HOME}/libexportPATH=${JAVA_HOME}/bin:$PATH四、验证通过以上步骤,JDK已安装完成。

E. java 如何用zlib解压缩tar.gz文件

public static void makeZip(List<File> fileList,String zipPath,boolean isDelete) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
File zipFile = new File(zipPath);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
// Compress the files
for (int i = 0; i < fileList.size(); i++) {
FileInputStream in = new FileInputStream(fileList.get(i));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(fileList.get(i).getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
System.out.println("压缩完成.");

//把旧的文件删除
if(isDelete == true){
for (int i = 0; i < fileList.size(); i++) {
File oldFile = fileList.get(i);
oldFile.delete();
}
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
File in1=new File("D:\\a.txt");
File in2=new File("D:\\b.txt");
File[] file=new File[]{in1,in2};
File zip=new File("D:\\ab.zip");
IDMZip mgr=new IDMZip();
mgr.ZipFiles(file, zip);
}

这个方法不管你是在windows下还是在linux下,都能正常执行。

F. 在LINUX下 用JAVA如何解压rar文件

下载地址:http://www.rarsoft.com/download.htm(目前最新为RAR 3.71 for Linux)

以最新的为准。

对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们:

1:对于.zip

linux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序。它们的参数选项很多,这里只做简单介绍,举例说明一下其用法:

# zip all.zip *.jpg(这条命令是将所有.jpg的文件压缩成一个zip包)

# unzip all.zip(这条命令是将all.zip中的所有文件解压出来)

2:对于.rar

要在linux下处理.rar文件,需要安装RAR for Linux,可以从网上下载,但要记住,RAR for Linux不是免费的;可从http://www.rarsoft.com/download.htm下载RAR 3。60 for Linux ,然后安装其安装操作如下:

# tar -xzpvf rarlinux-3.2.0.tar.gz
# cd rar
# make

这样就安装好了,安装后就有了rar和unrar这两个程序,rar是压缩程序,unrar是解压程序。它们的参数选项很多,举例说明一下其用法

# rar a all *.jpg

这条命令是将所有.jpg的文件压缩成一个rar包,名为all.rar,该程序会将.rar 扩展名将自动附加到包名后。

# unrar e all.rar

这条命令是将all.rar中的所有文件解压出来。

linux下的文件名引用的时候空格要加转义 比如

test file.rar 实际引用的时候就是 test\ file.rar

至于怎么解决 我就不再多说了哈

G. java里怎么解压tar.gz文件啊,网上好多例子都不行

我觉得你的步骤有问题,tar.gz压缩包里放文件或文件夹都无所谓,需要用程序来生成,下面详细说明:
1.
用程序中的方法【archive】生成tar压缩文件
2.
用程序中的方法【compressArchive】生成tar.gz压缩文件
3.
将生成的压缩文件为参数进行解压,具体是:
unCompressArchiveGz("d:\\test\\xmlbak.tar.gz");//解压
4.
查看解压后的文件夹内容和文件内容,均可以正常显示访问
楼主的问题主要是手动生成了一个压缩文件,这是主要的问题原因。

H. 如何安装tar.gz版本的java

总体步骤是“解压”然后“配置”,细节上讲:
1解压 :tar -zxvf java的tar.gz
2配置:vi /etc/profile //打开配置全局变量文件
export JAVA_HOME=刚才解压的目录
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
3.测试一下 输入java看是不是有反应,再试试java -version能不能读出java的版本号。
配置内容(就是export。。。)在网上有很多种,记得java_home的路径填对就好了。这个过程只有两部,第一步不可能出错,所以一定是配置的问题。

I. 如何解压.tar.gz gzip gz 类型文档

java解压缩.gz .zip .tar.gz等格式的压缩包方法总结
1、.gz文件是linux下常见的压缩格式。使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream

1 public static void unGzipFile(String sourcedir) {
2 String ouputfile = "";
3 try {
4 //建立gzip压缩文件输入流
5 FileInputStream fin = new FileInputStream(sourcedir);
6 //建立gzip解压工作流
7 GZIPInputStream gzin = new GZIPInputStream(fin);
8 //建立解压文件输出流
9 ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.'));
10 ouputfile = ouputfile.substring(0,ouputfile.lastIndexOf('.'));
11 FileOutputStream fout = new FileOutputStream(ouputfile);
12
13 int num;
14 byte[] buf=new byte[1024];
15
16 while ((num = gzin.read(buf,0,buf.length)) != -1)
17 {
18 fout.write(buf,0,num);
19 }
20
21 gzin.close();
22 fout.close();
23 fin.close();
24 } catch (Exception ex){
25 System.err.println(ex.toString());
26 }
27 return;
28 }

2、zip文件,使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile

1 /**
2 * 解压缩zipFile
3 * @param file 要解压的zip文件对象
4 * @param outputDir 要解压到某个指定的目录下
5 * @throws IOException
6 */
7 public static void unZip(File file,String outputDir) throws IOException {
8 ZipFile zipFile = null;
9
10 try {
11 Charset CP866 = Charset.forName("CP866"); //specifying alternative (non UTF-8) charset
12 //ZipFile zipFile = new ZipFile(zipArchive, CP866);
13 zipFile = new ZipFile(file, CP866);
14 createDirectory(outputDir,null);//创建输出目录
15
16 Enumeration<?> enums = zipFile.entries();
17 while(enums.hasMoreElements()){
18
19 ZipEntry entry = (ZipEntry) enums.nextElement();
20 System.out.println("解压." + entry.getName());
21
22 if(entry.isDirectory()){//是目录
23 createDirectory(outputDir,entry.getName());//创建空目录
24 }else{//是文件
25 File tmpFile = new File(outputDir + "/" + entry.getName());
26 createDirectory(tmpFile.getParent() + "/",null);//创建输出目录
27
28 InputStream in = null;
29 OutputStream out = null;
30 try{
31 in = zipFile.getInputStream(entry);;
32 out = new FileOutputStream(tmpFile);
33 int length = 0;
34
35 byte[] b = new byte[2048];
36 while((length = in.read(b)) != -1){
37 out.write(b, 0, length);
38 }
39
40 }catch(IOException ex){
41 throw ex;
42 }finally{
43 if(in!=null)
44 in.close();
45 if(out!=null)
46 out.close();
47 }
48 }
49 }
50
51 } catch (IOException e) {
52 throw new IOException("解压缩文件出现异常",e);
53 } finally{
54 try{
55 if(zipFile != null){
56 zipFile.close();
57 }
58 }catch(IOException ex){
59 throw new IOException("关闭zipFile出现异常",ex);
60 }
61 }
62 }
63
64 /**
65 * 构建目录
66 * @param outputDir
67 * @param subDir
68 */
69 public static void createDirectory(String outputDir,String subDir){
70 File file = new File(outputDir);
71 if(!(subDir == null || subDir.trim().equals(""))){//子目录不为空
72 file = new File(outputDir + "/" + subDir);
73 }
74 if(!file.exists()){
75 if(!file.getParentFile().exists())
76 file.getParentFile().mkdirs();
77 file.mkdirs();
78 }
79 }

3、.tar.gz文件可以看做先用tar打包,再使用gz进行压缩。
使用org.apache.tools.tar.TarEntry; org.apache.tools.tar.TarInputStream 和 org.apache.tools.tar.TarOutputStream

1 //------------------------------------------------------------------------------------------------------
2 /**
3 * 解压tar.gz 文件
4 * @param file 要解压的tar.gz文件对象
5 * @param outputDir 要解压到某个指定的目录下
6 * @throws IOException
7 */
8 public static void unTarGz(File file,String outputDir) throws IOException{
9 TarInputStream tarIn = null;
10 try{
11 tarIn = new TarInputStream(new GZIPInputStream(
12 new BufferedInputStream(new FileInputStream(file))),
13 1024 * 2);
14
15 createDirectory(outputDir,null);//创建输出目录
16
17 TarEntry entry = null;
18 while( (entry = tarIn.getNextEntry()) != null ){
19
20 if(entry.isDirectory()){//是目录
21 entry.getName();
22 createDirectory(outputDir,entry.getName());//创建空目录
23 }else{//是文件
24 File tmpFile = new File(outputDir + "/" + entry.getName());
25 createDirectory(tmpFile.getParent() + "/",null);//创建输出目录
26 OutputStream out = null;
27 try{
28 out = new FileOutputStream(tmpFile);
29 int length = 0;
30
31 byte[] b = new byte[2048];
32
33 while((length = tarIn.read(b)) != -1){
34 out.write(b, 0, length);
35 }
36
37 }catch(IOException ex){
38 throw ex;
39 }finally{
40
41 if(out!=null)
42 out.close();
43 }
44 }
45 }
46 }catch(IOException ex){
47 throw new IOException("解压归档文件出现异常",ex);
48 } finally{
49 try{
50 if(tarIn != null){
51 tarIn.close();
52 }
53 }catch(IOException ex){
54 throw new IOException("关闭tarFile出现异常",ex);
55 }
56 }
57 }

使用到的包头有:

1 import java.io.BufferedInputStream;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import java.nio.charset.Charset;
10 import java.util.Enumeration;
11 import java.util.zip.GZIPInputStream;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipFile;
14
15 import org.apache.tools.tar.TarEntry;
16 import org.apache.tools.tar.TarInputStream;
17 import org.apache.tools.tar.TarOutputStream;

热点内容
编程sqs 发布:2025-02-12 11:09:55 浏览:238
electron脱离编译环境 发布:2025-02-12 11:08:21 浏览:68
安卓一体机喇叭插口在哪里 发布:2025-02-12 11:07:37 浏览:866
广东江门首选dns服务器地址 发布:2025-02-12 10:56:55 浏览:954
台湾大量IP服务器 发布:2025-02-12 10:51:43 浏览:375
sship访问服务器 发布:2025-02-12 10:50:16 浏览:98
人机局脚本 发布:2025-02-12 10:48:03 浏览:66
安卓哪里可以下2k17 发布:2025-02-12 10:45:36 浏览:317
m1二级缓存才4m 发布:2025-02-12 10:36:43 浏览:177
编译器一直返回error 发布:2025-02-12 10:25:27 浏览:556