java程序解压/压缩.gz文件

简介: 压缩成.gz格式import java.io.FileInputStream; import java.io.FileNotFoundException; import java.

压缩成.gz格式

import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.FileOutputStream;   
import java.io.IOException;   
import java.util.zip.GZIPOutputStream;   
  
  
public class CompressFileGZIP {   
private static void doCompressFile(String inFileName) {   
    
        try {   
          
            System.out.println("Creating the GZIP output stream.");   
            String outFileName = inFileName + ".gz";   
            GZIPOutputStream out = null;   
            try {   
                out = new GZIPOutputStream(new FileOutputStream(outFileName));   
            } catch(FileNotFoundException e) {   
                System.err.println("Could not create file: " + outFileName);   
                System.exit(1);   
            }   
                      
    
            System.out.println("Opening the input file.");   
            FileInputStream in = null;   
            try {   
                in = new FileInputStream(inFileName);   
            } catch (FileNotFoundException e) {   
            System.err.println("File not found. " + inFileName);   
                System.exit(1);   
            }   
  
            System.out.println("Transfering bytes from input file to GZIP Format.");   
            byte[] buf = new byte[1024];   
            int len;   
            while((len = in.read(buf)) > 0) {   
                out.write(buf, 0, len);   
            }   
            in.close();   
  
            System.out.println("Completing the GZIP file");   
            out.finish();   
            out.close();   
          
        } catch (IOException e) {   
            e.printStackTrace();   
            System.exit(1);   
        }   
  
    }   
  
    /**  
     * Sole entry point to the class and application.  
     * @param args Array of String arguments.  
     */   
    public static void main(String[] args) {   
    String str="E:\\AUTORUN.INF";   
          
            doCompressFile(str);   
          
    
               
    }   
}   

解压.gz格式

import java.util.zip.GZIPInputStream;   
import java.io.FileOutputStream;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.IOException;   
  
public class UncompressFileGZIP {   
  
    /**  
     * Uncompress the incoming file.  
     * @param inFileName Name of the file to be uncompressed  
     */   
    private static void doUncompressFile(String inFileName) {   
  
        try {   
  
            if (!getExtension(inFileName).equalsIgnoreCase("gz")) {   
                System.err.println("File name must have extension of \".gz\"");   
                System.exit(1);   
            }   
  
            System.out.println("Opening the compressed file.");   
            GZIPInputStream in = null;   
            try {   
                in = new GZIPInputStream(new FileInputStream(inFileName));   
            } catch(FileNotFoundException e) {   
                System.err.println("File not found. " + inFileName);   
                System.exit(1);   
            }   
  
            System.out.println("Open the output file.");   
            String outFileName = getFileName(inFileName);   
            FileOutputStream out = null;   
           try {   
                out = new FileOutputStream(outFileName);   
            } catch (FileNotFoundException e) {   
                System.err.println("Could not write to file. " + outFileName);   
                System.exit(1);   
            }   
  
            System.out.println("Transfering bytes from compressed file to the output file.");   
            byte[] buf = new byte[1024];   
            int len;   
            while((len = in.read(buf)) > 0) {   
                out.write(buf, 0, len);   
            }   
  
            System.out.println("Closing the file and stream");   
            in.close();   
            out.close();   
          
        } catch (IOException e) {   
            e.printStackTrace();   
            System.exit(1);   
        }   
  
    }   
  
    /**  
     * Used to extract and return the extension of a given file.  
     * @param f Incoming file to get the extension of  
     * @return <code>String</code> representing the extension of the incoming  
     *         file.  
     */   
    public static String getExtension(String f) {   
        String ext = "";   
        int i = f.lastIndexOf('.');   
  
        if (i > 0 &&  i < f.length() - 1) {   
            ext = f.substring(i+1);   
        }        
        return ext;   
    }   
  
    /**  
     * Used to extract the filename without its extension.  
     * @param f Incoming file to get the filename  
     * @return <code>String</code> representing the filename without its  
     *         extension.  
     */   
    public static String getFileName(String f) {   
        String fname = "";   
        int i = f.lastIndexOf('.');   
  
        if (i > 0 &&  i < f.length() - 1) {   
            fname = f.substring(0,i);   
        }        
        return fname;   
    }   
  
    /**  
     * Sole entry point to the class and application.  
     * @param args Array of String arguments.  
     */   
    public static void main(String[] args) {   
      
         
            doUncompressFile("E:\\AUTORUN.INF.gz");   
         
  
    }   
  
}   

  本文出处:http://panshaobinsb.iteye.com/blog/1566231

目录
相关文章
|
24天前
|
Java Apache 开发工具
【Azure 事件中心】 org.slf4j.Logger 收集 Event Hub SDK(Java) 输出日志并以文件形式保存
【Azure 事件中心】 org.slf4j.Logger 收集 Event Hub SDK(Java) 输出日志并以文件形式保存
|
13天前
|
Arthas Java 测试技术
Java字节码文件、组成,jclasslib插件、阿里arthas工具,Java注解
Java字节码文件、组成、详解、分析;常用工具,jclasslib插件、阿里arthas工具;如何定位线上问题;Java注解
Java字节码文件、组成,jclasslib插件、阿里arthas工具,Java注解
|
12天前
|
Java API 开发者
【Java字节码操控新篇章】JDK 22类文件API预览:解锁Java底层的无限可能!
【9月更文挑战第6天】JDK 22的类文件API为Java开发者们打开了一扇通往Java底层世界的大门。通过这个API,我们可以更加深入地理解Java程序的工作原理,实现更加灵活和强大的功能。虽然目前它还处于预览版阶段,但我们已经可以预见其在未来Java开发中的重要地位。让我们共同期待Java字节码操控新篇章的到来!
|
10天前
|
Java API 开发者
【Java字节码的掌控者】JDK 22类文件API:解锁Java深层次的奥秘,赋能开发者无限可能!
【9月更文挑战第8天】JDK 22类文件API的引入,为Java开发者们打开了一扇通往Java字节码操控新世界的大门。通过这个API,我们可以更加深入地理解Java程序的底层行为,实现更加高效、可靠和创新的Java应用。虽然目前它还处于预览版阶段,但我们已经可以预见其在未来Java开发中的重要地位。让我们共同期待Java字节码操控新篇章的到来,并积极探索类文件API带来的无限可能!
|
11天前
|
算法 Java
Java 压缩文件
在Java中压缩文件是一个常见的需求,通常可以通过使用Java自带的`java.util.zip`包来实现。这个包提供了`ZipOutputStream`类来创建ZIP格式的压缩文件。以下是一个简单的示例,展示了如何将多个文件压缩到一个ZIP文件中。 ### 示例:将多个文件压缩到一个ZIP文件中 ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipFilesExample { public static vo
|
16天前
|
缓存 监控 安全
如何提高 Java 高并发程序的性能?
以下是提升Java高并发程序性能的方法:优化线程池设置,减少锁竞争,使用读写锁和无锁数据结构。利用缓存减少重复计算和数据库查询,并优化数据库操作,采用连接池和分库分表策略。应用异步处理,选择合适的数据结构如`ConcurrentHashMap`。复用对象和资源,使用工具监控性能并定期审查代码,遵循良好编程规范。
|
23天前
|
Java 应用服务中间件 HSF
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
Java应用结构规范问题之配置Logback以仅记录错误级别的日志到一个滚动文件中的问题如何解决
|
23天前
|
Java 程序员
Java系统中的错误码设计问题之实现一个基于properties文件的错误消息管理系统如何解决
Java系统中的错误码设计问题之实现一个基于properties文件的错误消息管理系统如何解决
12 1
|
18天前
|
Java
java判断文件内容不为空
请注意,在实际的生产环境中,处理文件时需要考虑异常处理。在上述代码中,如果在文件读取过程中发生 `IOException`,则会被捕获,并在控制台打印堆栈跟踪信息。在更复杂的系统中,可能还需要更精细的异常处理策略。
28 0
|
24天前
|
Java Linux C++
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑