压缩java字符串(urls)

Mor*_* B. 2 java compression string url web-crawler

我有很多网址要处理.我在一个哈希集中存储了大约20'000'000.这会造成一些记忆问题.

我试图创建一个压缩的字符串类:

import java.io.*;//file writer
import java.util.*;
import java.util.zip.*;

class CompressedString2 implements Serializable{
    private int originalSize;
    private byte[] cstring;



    public CompressedString2 (){
        compress("");
    }


    public CompressedString2 (String string){
        compress(string);
    }


    public void compress(String str){
        try {
            byte[] bytes = str.getBytes("UTF-8");
            originalSize = bytes.length;

            ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
            DeflaterOutputStream dos = new DeflaterOutputStream(deflatedBytes,new Deflater(Deflater.DEFAULT_COMPRESSION));
            dos.write(bytes);
            dos.finish();
            cstring=deflatedBytes.toByteArray();
        }catch(Exception e){e.printStackTrace();}

    }


    public String decompress() throws Exception{
        String result="";
        try{
            ByteArrayOutputStream deflatedBytes=new ByteArrayOutputStream();
            deflatedBytes.write(cstring);
            deflatedBytes.close();


            InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(deflatedBytes.toByteArray()));
            byte[] inflatedBytes = new byte[originalSize];
            iis.read(inflatedBytes);
            result= new String(inflatedBytes, "UTF-8");
        }catch(Exception e){e.printStackTrace();}
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

但事实上当我用这样的东西存储它们时:

HashSet<String> urlStr=new HashSet<String>();
HashSet<CompressedString> urlComp=new HashSet<CompressedString>();


        String filePath=new String();

            filePath=args[0];

        int num=0;

        try{
            BufferedReader br = new BufferedReader(new FileReader(filePath));

            String line = br.readLine();
            while (line != null) {

                num++;
                urlStr.add(line);
                urlComp.add(new CompressedString(line));

            line = br.readLine();
            }
        } catch(Exception e){
        System.out.println("fehler..:");
            e.printStackTrace();
        }

ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("testDeflator_rawurls.obj"));
oos1.writeObject(urlStr);
ObjectOutputStream oos4 = new ObjectOutputStream(new FileOutputStream("testDeflator_compressed2.obj"));
oos4.writeObject(urlComp);
Run Code Online (Sandbox Code Playgroud)

"压缩"的网址甚至更大......

有没有人知道如何成功压缩网址?

Ern*_*ill 5

好吧,如果他们在一个集合中,那么你所能做的就是添加/删除/查找.您还可以在"角色森林"上执行这些操作,它可以是更紧凑的表示.我在想一个节点树,每个节点都有一个字符,彼此相连.森林的根将包含"h","f"等等.在"h"节点下将是"t"节点,并且在该节点下是另一个"t",并且在该节点下面是"p"等."f"节点将具有"t"和"i"子节点.最终树会分支,但根部附近可能会有很多共享.然后,您只需走过森林,看看是否有URL.

我想一个节点需要一个布尔成员来指示集合中的一个URL终止,一个用于保存字符的成员,以及一个指向其他节点的链接数组.

  • +1.根据URL,"字符串林"可能会更好(在查找时间和存储开销方面).像"http://"和"ftp://"之类的根源是儿童在每个"/"处分开. (2认同)