-1

現在、ユーザーがファイルを圧縮および解凍できるようにするアプリケーションを Java/Android で開発しています。最初は、次のようなファイルサイズの調査を開始しました。

1Byte = 8Bits
1KB = 1024Byte
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
1EB = 1024PB
1ZB = 1024EB
1YB = 1024ZB

これを調べた後、ネット上のいくつかの記事を調べて読んだところ、ファイル圧縮には 2 種類あることがわかりました (間違っていたら訂正してください): Lossless と Lossy です。可逆圧縮とは、ファイルを 1 つも失わずに小さなビットに圧縮することを意味し、非可逆圧縮とは、ファイルの圧縮中に重要なファイルが削除されたことを意味します。

また、圧縮(ランレングスコーディング方式)は次のようになっていることも読みました。

AAABBCCDFFFFEEEEH

これに:

3A2B2CD4F4EH  

これにより、ファイルの圧縮/解凍がどのように機能するかがわかります。

私はまた、Java(Androidにも適用可能)でファイルを圧縮するためのAPIがあることをネットで検索しました。

java.util.zip

また、さまざまな役立つ Web サイト/フォーラム/など (stackoverflow.com を含む) からファイルを圧縮および解凍するためのコードをいくつか試しました。これにより、この調査の経験が得られます。

また、データ圧縮で使用されるアルゴリズムについても読みました。

 Huffman encoding algorithm -  assigns a code to characters in a file based on how frequently those characters occur

run-length encoding - generates a two-part value for repeated characters: the first part specifies the number of times the character is repeated, and the second part identifies the character

Lempel-Ziv algorithm - converts variable-length strings into fixed-length codes that consume less space than the original strings.

ここで、java.util.zip を使用してファイルを圧縮および解凍する際にアルゴをコーディングする方法を知る必要があります (使用方法もわかりません。ネット上のチュートリアルは私にとっては機能しません :/)。winzip、winrar、圧縮フォルダー (windows)、androzip (android アプリ) はどのアルゴリズムを使用していますか? java.util.zip の仕組みとさまざまなアルゴリズムについて、誰かが段階的に教えてくれませんか (学校に通っていない人として扱ってください)。長い投稿でごめんなさい。今後のヘルプと投稿 (ある場合) に感謝します!

4

1 に答える 1

0
public static final byte[] unzip(byte[] in) throws IOException {
// decompress using GZIPInputStream 
ByteArrayOutputStream outStream = 
  new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

GZIPInputStream inStream = 
  new GZIPInputStream ( new ByteArrayInputStream(in) );

byte[] buf = new byte[BUF_SIZE];
while (true) {
  int size = inStream.read(buf);
  if (size <= 0) 
    break;
  outStream.write(buf, 0, size);
}
outStream.close();

return outStream.toByteArray();
}


public static final byte[] zip(byte[] in) {
try {
  // compress using GZIPOutputStream 
  ByteArrayOutputStream byteOut= 
    new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

  GZIPOutputStream outStream= new GZIPOutputStream(byteOut);

  try {
    outStream.write(in);
  } catch (Exception e) {
    LOG.error("", e);
  }

  try {
    outStream.close();
  } catch (IOException e) {
      LOG.error("", e);
  }

  return byteOut.toByteArray();

} catch (IOException e) {
    LOG.error("", e);
  return null;
}
}
于 2012-06-05T04:39:50.817 に答える