日期:2014-05-20  浏览次数:20604 次

J2ME如何解压带密码的zip包,已经知道密码
J2ME如何解压带密码的zip包,已经知道密码。
我这里是要在手机里面打开 zip包。

------解决方案--------------------
只知道解压没密码。不过有款J2ME软件可以解压带密码的zip。
BlueFTP可以
------解决方案--------------------
超级短小精悍的ZIP解压缩类,只有280行Java代码,混淆并压缩后的class文件仅4K左右,特别适用于j2me开发。
解压缩算法来自网上那个著名的GZIP.java,只添加了解析ZIP文件格式的部分。
list()方法:
列出zip包中所有的文件及目录,列出的是包括路径的全名,比如文件"dir1/dir2/file.txt",或目录"dir1/dir2/",注意路径分隔符是正斜杠'/',目录的最后一个字符一定是'/'。
get(String filename)方法:
从zip包中解压缩给定名字的文件,返回字节数组。
ZipMe(InputStream is):
从一个输入流读取zip文件,可以是jar包中的文件,也可以是通过FileConnection获取的手机文件系统中的文件,或从网络上下载的zip包。
下面的例子代码访问jar包中的zip文件并逐一解压缩包中的所有文件:
InputStream is = "".getClass().getResourceAsStream("/res/test.zip"); 
ZipMe file = new ZipMe(is);
is.close();
Vector p = file.list();
for (int i = 0; i < p.size(); i++) {
String name = (String) p.get(i);
byte[] bb = file.get(name);
System.out.println("file: " + name + " / data_len: " + (bb != null ? bb.length : -1));
}

------解决方案--------------------
Java code


import java.io.*;
import java.util.*;

public class ZipMe {

   private static final int BTYPE_NONE = 0;
   private static final int BTYPE_DYNAMIC = 2;
   private static final int MAX_BITS = 16;
   private static final int MAX_CODE_LITERALS = 287;
   private static final int MAX_CODE_DISTANCES = 31;
   private static final int MAX_CODE_LENGTHS = 18;
   private static final int EOB_CODE = 256;

   private static final int LENGTH_EXTRA_BITS[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1,
           1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 };
   private static final int LENGTH_VALUES[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11,
           13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
           163, 195, 227, 258, 0, 0 };
   private static final int DISTANCE_EXTRA_BITS[] = { 0, 0, 0, 0, 1, 1, 2, 2,
           3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12,
           13, 13 };
   private static final int DISTANCE_VALUES[] = { 1, 2, 3, 4, 5, 7, 9, 13, 17,
           25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049,
           3073, 4097, 6145, 8193, 12289, 16385, 24577 };
   private static final int DYNAMIC_LENGTH_ORDER[] = { 16, 17, 18, 0, 8, 7, 9,
           6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };

   private int curIndex, curByte, curBit;

   public final  byte[] inflate(byte data[], int startIdx, int size) {
       curIndex = startIdx;
       byte ret[] = new byte[size];
       int idx, bfinal, btype;
       idx = bfinal = btype = curByte = curBit = 0;
       do {
           bfinal = readBits(data, 1);
           btype = readBits(data, 2);
           if (btype == BTYPE_NONE) {
               curBit = 0;
               // LEN.
               int len = readBits(data, 16);
               // NLEN.
               readBits(data, 16);
               System.arraycopy(data, curIndex, ret, idx, len);
               curIndex += len;
               idx += len;
           } else {
               int literalTree[], distanceTree[];
               if (btype == BTYPE_DYNAMIC) {
                   int hlit = readBits(data, 5) + 257;
                   int hdist = readBits(data, 5) + 1;
                   int hclen = readBits(data, 4) + 4;
                   byte lengthBits[] = new byte[MAX_CODE_LENGTHS + 1];
                   for (int i = 0; i < hclen; i++)
                       lengthBits[DYNAMIC_LENGTH_ORDER[i]] = (byte) readBits(data, 3);
                   int lengthTree[] = huffmanTree(lengthBits, MAX_CODE_LENGTHS);
                   literalTree = huffmanTree(codeLengths(data,
                           lengthTree, hlit), hlit - 1);
                   distanceTree = huffmanTree(codeLengths(data,
                           lengthTree, hdist), hdist - 1);
               } else {
                   byte literalBits[] = new byte[MAX_CODE_LITERALS + 1];
                   for (int i = 144; --i >= 0; literalBits[i] = 8);
                   for (int i = 256; --i >= 144; literalBits[i] = 9);
                   for (int i = 280; --i >= 256; literalBits[i] = 7);
                   for (int i = 288; --i >= 280; literalBits[i] = 8);
                   literalTree = huffmanTree(literalBits, MAX_CODE_LITERALS);

                   byte distanceBits[] = new byte[MAX_CODE_DISTANCES + 1];
                   for (int i = distanceBits.length; --i >= 0; distanceBits[i] = 5);
                   distanceTree = huffmanTree(distanceBits, MAX_CODE_DISTANCES);
               }
               int code = 0, leb = 0, deb = 0;
               while ((code = readCode(data, literalTree)) != EOB_CODE) {
                   if (code > EOB_CODE) {
                       code -= 257;
                       int length = LENGTH_VALUES[code];
                       if ((leb = LENGTH_EXTRA_BITS[code]) > 0)
                           length += readBits(data, leb);
                       code = readCode(data, distanceTree);
                       int distance = DISTANCE_VALUES[code];
                       if ((deb = DISTANCE_EXTRA_BITS[code]) > 0)
                           distance += readBits(data, deb);
                       int offset = idx - distance;
                        while (distance < length) {
                            System.arraycopy(ret, offset, ret, idx, distance);
                            idx += distance;
                            length -= distance;
                            distance <<= 1;
                        }
                        System.arraycopy(ret, offset, ret, idx, length);
                        idx += length;
                    } else {
                        ret[idx++] = (byte) code;
                    }
                }
            }
        } while (bfinal == 0);
        return ret;
    }
 
    private final int readBits(byte bb[], int n) {
        int data = (curBit == 0 ? (curByte = (bb[curIndex++] & 0xFF))
                : (curByte >> curBit));
        for (int i = (8 - curBit); i < n; i += 8) {
            curByte = (bb[curIndex++] & 0xFF);
            data |= (curByte << i);
        }
        curBit = (curBit + n) & 7;
        return (data & ((1 << n) - 1));
    }
 
    private final int readCode(byte bb[], int tree[]) {
        int node = tree[0];
        while (node >= 0) {
            if (curBit == 0) curByte = (bb[curIndex++] & 0xFF);
            node = (((curByte & (1 << curBit)) == 0) ? tree[node >> 16]
                    : tree[node & 0xFFFF]);
            curBit = (curBit + 1) & 7;
        }
        return (node & 0xFFFF);
    }