Jump to content

[SOLVED] Trouble with compress and decompress xml file


photo

Recommended Posts

Posted

Hi I am trying to reduce the size of xml file, and I successfully save a xml binary zip file :

original xml : 10,6 Mo
binary xml : 2,5 Mo

zip binary xml : 272 Ko

 

For have this result I just do : 

    {
        xml.save("/Users/aliot/Desktop/file.xml.bin",1);
        
        File fin = new File("/Users/aliot/Desktop/file.xml.bin","rb");
        int data[0];
        fin.read(data,fin.getSize());
        
        MESSAGE("File input %d\n",fin.getSize());
        
        Buffer buff = new Buffer();
        buff.write(data,fin.getSize());

        fin.close();
        delete fin;
        
        File fout = new File("/Users/aliot/Desktop/file.xml.bin.zip","wb");
        buff.compress(fout,1);
        delete buff;
        
        fout.close();
        delete fout;
    }

BTW may be will be nice to easily zip file using compress, anyway my trouble it's about do the reverse, when i try I have Buffer::decompress(): can't decompress data
 

    {
        File fin = new File("/Users/aliot/Desktop/file.xml.bin.zip","rb");
        int data[0];
        
        MESSAGE("File input %d\n",fin.getSize());
        MESSAGE("File decompress size %d\n",fin.readInt());
        
        fin.seekSet(0);
        fin.read(data,fin.getSize());

        fin.seekSet(0);
        
        Buffer buff = new Buffer();
        buff.write(data,fin.getSize());
        
        fin.close();
        delete fin;
        
        File fout = new File("/Users/aliot/Desktop/decompress.xml","wb");
        
        buff.decompress(fout);
        delete buff;
        
        fout.close();
        delete fout;
    }

I print some info for try to see if I have error : 

File input 2485111
File input compress 271847
File decompress size 2485111 (readInt on the compress file)

Why I can't decompress ??? do you make a mistake ?

 

Thanks 

 

Tony


 

 

Posted

Hi, I found my trouble, I misunderstand how decompress and use this function. The good code must be : 

 

    engine.filesystem.addBufferFile("decompress.xml");
    
    {
        File fin = new File(filename,"rb");
        
        Buffer buff = new Buffer();
        buff.decompress(fin);
        
        fin.close();
        delete fin;
        
        int data[0];
        buff.read(data,buff.getSize());
        
        File fout = new File("decompress.xml","wb");
        fout.write(data,buff.getSize());
        
        delete buff;
        
        fout.close();
        delete fout;
    }
    
    _template = new Xml();
    _template.load("decompress.xml");
    
    engine.filesystem.removeBufferFile("decompress.xml");

×
×
  • Create New...