[Javaの] 360はまだそれを使用して圧縮されましたか?独自のバーを書きます

圧縮されたデータストリーム形式を読み書きするJava I / Oのサポート、我々は圧縮と解凍の目的を達成するためにカプセル化することができます。
これらのクラスは継承されたリーダー、ライターからではなく、継承システムののInputStream、OutputStreamの部分に属します。彼らは、バイトでの操作に直面していること。我々は文字指向に動作している場合は、InputStreamReaderの、のOutputStreamWriterを簡単に変換することができます。

圧縮クラス 機能
CheckedInputStream GetCheckSum()とパリティの任意のinputStream
CheckOutputStream GetCheckSum()とパリティの任意のOutputStream
DeflaterOutputStream ベース・クラスの圧縮
ZipOutputStream DeflaterOutputStream実装クラス、データ・ストリームは、ZIPファイル形式で圧縮されてもよいです
GZIPOutputStream DeflaterOutputStream実装クラス、データ・ストリームは、GZIPファイル形式に圧縮することができます
InflaterInputStream 解凍基底クラスのクラス
ZipInputStream InfalterInputStream実装クラスは、ジップファイル形式を解凍することができます
GZIPInputStream InfalterInputStream実装クラスは、ジップファイル形式を解凍することができます

GZIP圧縮伸長フォーマット
GZIPは、単一のファイル圧縮の圧縮および圧縮解除に適し比較的簡単です。

package com.mfs.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPCompress {

	/*
	 * 压缩
	 * 参数: file指定要压缩的文件,path指定压缩后的文件的存储路径
	 * 返回值:返回压缩后的文件的绝对路径
	 */
	public String compress(String file,String path) throws IOException {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(file)));     
		BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(new File(path + "/GZIPCompress.gz"))));
		byte[] b = new byte[1024];
		int n = 0;
		while ((n = in.read(b)) != -1) {
			out.write(b, 0, n);
		}
		in.close();
		out.close();
		return new File(path + "/GZIPcompress.gz").getAbsolutePath();   
	}
	/*
	 * 解压缩
	 * 参数: 要解压缩的目标文件
	 */
	public void deCompress (String file) throws FileNotFoundException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(new File(file))))); //使用InputStream把面向字节流的对象转换成面向字符流的对象
		String s;
		while ((s = in.readLine()) != null) {
			System.out.println(s);
		}
		in.close();
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		GZIPCompress gz = new GZIPCompress();
		String GZIPFile = gz.compress("src/com/mfs/io/GZIPCompress.java", "resource");   //将当前文件压缩压缩到本项目的resource文件夹下
		gz.deCompress(GZIPFile);  //解压缩压缩后的文件
	}

}

ジップファイル圧縮および圧縮の解凍
Zipは、圧縮されたファイルは、標準のzip基準に従うことが圧縮ソフトウェア市場と協力するのは簡単ですので、それは、複数の圧縮ファイルをサポートすることができ、別のクラスは、非常に包括的な含まれています。

package com.mfs.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipCompress {

	/*
	 * 压缩
	 * 参数: file指定要压缩的文件,path指定压缩后的文件的存储路径
	 * 返回值:返回压缩后的文件的绝对路径
	 */
	public String compress (String[] file,String path) throws IOException {
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(path + "/ZipCompress.zip")));
		CheckedOutputStream csum = new CheckedOutputStream(zos, new Adler32()); //用于产生校验和,可以为任何InputStream产生校验和,有两种方法Adler32(快)、CRC32(慢,准确)
		BufferedOutputStream out = new BufferedOutputStream(csum);
		for (String f : file) {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(f)));
			zos.putNextEntry(new ZipEntry(f));   //每次向压缩文件夹中加入新的文件必掉用putNextEntry()方法,参数为ZipEntry对象
			int n = 0;
			while ((n = in.read()) != -1) {
				out.write(n);
			}
			in.close();
		}
		out.close();
		//System.out.println(csum.getChecksum().getValue()); //校验和
		return new File(path + "/ZipCompress.zip").getAbsolutePath();
	}
	/*
	 * 解压缩
	 * 参数: 要解压缩的目标文件
	 */
	public void deCompress (String file) throws IOException {
		ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(file)));
		CheckedInputStream csum = new CheckedInputStream(zis, new Adler32());   //校验和
		BufferedReader in = new BufferedReader(new InputStreamReader(csum));
		ZipEntry ze;
		while ((ze = zis.getNextEntry()) != null) {  
			String s;
			while ((s = in.readLine()) != null) {
				System.out.println(s);
			}
		}
		//System.out.println(csum.getChecksum().getValue());
		in.close();
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String[] file = new String[] {"src/com/mfs/io/ZipCompress.java","src/com/mfs/io/GZIPCompress.java"};
		ZipCompress zip = new ZipCompress();
		String zipFile = zip.compress(file, "resource");
		zip.deCompress(zipFile);
	}

}

各ファイルがアーカイブに追加されるために、あなたはputNextEntry()を呼び出し、ZipEntryオブジェクトを渡す必要があります。ZipEntryオブジェクトは、オブジェクトのための非常に包括的なインターフェース、十分に良いが含まれている、我々は簡単にセットアップし、ファイル情報に対応した圧縮ファイルを使用することができ、例えば:名前、圧縮と非圧縮ファイルサイズ、日付、CRC検証および(またはZipEntryはAdler32チェックをサポートしていません)。
ファイル解凍し、より簡単な方法は、ZIPファイルの種類を使用することですがあります:

public void deCompress2 (String file) throws ZipException, IOException {
		ZipFile zip = new ZipFile(new File(file));
		Enumeration<? extends ZipEntry> entries = zip.entries();
		while (entries.hasMoreElements()) {
			ZipEntry ze = entries.nextElement();
			BufferedReader in = new BufferedReader(new InputStreamReader(zip.getInputStream(ze)));
			String s;
			while ((s = in.readLine()) != null) {
				System.out.println(s);
			}
			in.close();
		}
	}

setCommentジップストリーム()メソッドは、いくつかのコメントを追加できますが、混乱しているが、我々は、その後、より良いのZipEntry内部のコメントや置くのコメントを取得したい場合は、コメントを取得するには何の方法は、ありません。

公開された57元の記事 ウォン称賛55 ビュー1928

おすすめ

転載: blog.csdn.net/qq_40561126/article/details/104974459