java读取gz压缩文件|gz后缀怎么解压

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

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

2. gz后缀怎么解压

1、在系统环境下,安装解压软件

3. java如何解压.gz后缀的压缩包

File file = new File(zipFilePath); 将zip文件路径转换 成文件

zipFile = new ZipFile(file); 调用java util下面的zipfile类

Enumeration<?> zipEnum = zipFile.entries(); 将zip文件里面的内容都放在迭代器里面了

ZipEntry entry = (ZipEntry) zipEnum.nextElement();,然后迭代出ZipEntry对象。

zipFile.getInputStream(entry)就可以得到所需要的流了,之后做你需要的操作。

4. 如何通过java,不进行解压就把iso、apk、gz等压缩文件中的文件名读取出来求可行的思路!谢谢!

对着压缩包,右JI,通过WINRAR打开,通过左键直接把文件拖出来,再重命名,再拖进去,就行了

5. java spark获取gz文件时怎样分区

spark 读取HDFS文件,首先会判断文件是否可分,可切分即可分成多个partition,不可切分则整个文件为一个partition ,gz等压缩文件属于不可切分文件,所以设置分区无效,如果想要可变分区,需要对文件加索引支持可切分

6. ".tar"是什么格式,要如何打开

要打开TAR,使用Java APIJava API提供了解压缩GZIP和ZIP文件的功能。然而还有许多其他的压缩格式它并不支持。其中一种最普通格式就是TAR文档格式。这里有一个可以通过Java来读.tar文件的API。Ice Tar API和java.util.zip API的使用方法很相似。一个.tar文件通过TarInputStream被读取为一系列TarEntry对象。一个TarEntry对象具有多种运算和特征信息,包括名称,是否是个目录,以及它所包含的数据。以下是一个简单的代码片断:String filename = "somefile.tar"; String directory = "somedirectory/"; OutputStream out = null; try { // open up the .tar file TarInputStream in = new TarInputStream( new FileInputStream(new File(filename) ) ); TarEntry entry = null; // loop over each file/directory in the .tar while( (entry = in.getNextEntry()) != null) { // ignore directories if(entry.isDirectory()) { continue; } // create a file to output to File outfile = new File(directory+entry.getName()); // make any missing directories new File(outfile.getParent()).mkdirs(); // create an output stream to write to out = new BufferedOutputStream( new FileOutputStream( outfile ) ); // write out the tar-entry int x = 0; while( (x = in.read()) != -1) { out.write(x); } out.close(); } in.close(); } catch(IOException ioe) { ioe.printStackTrace(); // close off streams etc.. }下面介绍一个有用的指令:File.mkdirs()。我们来假设一个路径:/home/javauser/com/generationjava/files/Example.java事实上,现在只有/home/javauser/是已经存在的,那么mkdirs()将生成com/, generationjava/, 和 files/这些目录。Ice Tar API使得应用Java的人不仅仅可以处理ZIP文档,而且还可以处理其他格式的文档。通过把java.util.zip.GzipInputStream应用到Ice Tar API中,tar.gz文件就可以很容易的读取了。

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

我觉得你的步骤有问题,tar.gz压缩包里放文件或文件夹都无所谓,需要用程序专来生成,属下面详细说明:

用程序中的方法【archive】生成tar压缩文件

用程序中的方法【compressArchive】生成tar.gz压缩文件

将生成的压缩文件为参数进行解压,具体是:

unCompressArchiveGz("d:\test\xmlbak.tar.gz");//解压

查看解压后的文件夹内容和文件内容,均可以正常显示访问

楼主的问题主要是手动生成了一个压缩文件,这是主要的问题原因。

8. 用java如何解析gz文件

一个偷懒的做法是调用操作系统命令把gz解压缩,然后再读取。网上也许能找到一些操作gz的java库。

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

java解压缩.gz .zip .tar.gz等格式的压缩包方法总结1、.gz文件是linux下常见的压缩格式。使用 java.util.zip.GZIPInputStream即可,压缩是 java.util.zip.GZIPOutputStream1 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.ZipFile1 /**2 * 解压缩zipFile3 * @param file 要解压的zip文件对象4 * @param outputDir 要解压到某个指定的目录下5 * @throws IOException6 */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) charset12 //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.TarOutputStream1 //——————————————————————————————————2 /**3 * 解压tar.gz 文件4 * @param file 要解压的tar.gz文件对象5 * @param outputDir 要解压到某个指定的目录下6 * @throws IOException7 */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;89 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;

10. 如何让Hadoop读取以gz结尾的文本格式的文件

分析过程:通过上面的异常,立马猜想到是由于我的文件是gz结尾,所以hadoop把它当作了压缩文件,然后尝试解压缩后读取,所以解压失败了。于是去问google,没有搜到能够直接解决我问题的答案,但是搜到了此处相关的源代码:LineRecordReader.java;于是尝试着去阅读代码来解决问题,这个类很简单,继承自RecordReader,没有看到next函数和readLine函数,那就应该是基类实现的。很快发现了看名字是跟压缩解码相关的代码:private CompressionCodecFactory compressionCodecs = null;…compressionCodecs = new CompressionCodecFactory(job);final CompressionCodec codec = compressionCodecs.getCodec(file);…if (codec != null) { in = new LineReader(codec.createInputStream(fileIn), job);}else{ … in = new LineReader(fileIn, job);} 此处file就是拿到的文件路径,可以看到,应该就是通过CompressionCode.getCode(file)函数,拿到的codec类,然后读取的时候出异常了。那怎么让MapRece程序把这个.gz文件当作普通的文本文件呢?再点进去看CompressionCodeFactory.java的代码。getCodec函数的代码如下:/*** Find the relevant compression codec for the given file based on its* filename suffix.* @param file the filename to check* @return the codec object*/public CompressionCodec getCodec(Path file) { CompressionCodec result = null; if (codecs != null) { String filename = file.getName(); String reversedFilename = new StringBuffer(filename).reverse().toString(); SortedMap<String, CompressionCodec> subMap = codecs.headMap(reversedFilename); if (!subMap.isEmpty()) { String potentialSuffix = subMap.lastKey(); if (reversedFilename.startsWith(potentialSuffix)) { result = codecs.get(potentialSuffix); } } } return result;}


赞 (0)