edmund-wagner / junrar

plain java unrar util (former sf project)
Other
186 stars 90 forks source link

Unrar of a byte array #18

Open AndreaSpinelli opened 11 years ago

AndreaSpinelli commented 11 years ago

Hi,

I did not find a buit-in way of unrar'ing a byte array, so I wrote an implementation of VolumeManager and one of Volume, called ByteArrayVolumeManager and ByteArrayVolume.

I think they may be useful to someone else, so I donate them to the project ;-)

I ask you, anyway, to check that I did not do any large and stupid error (my tests are ok, though).

I paste them here:

/*
 * This file is part of seedbox <github.com/seedbox>.
 *
 * seedbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * seedbox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with seedbox.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.github.junrar.impl;

import java.io.IOException;

import com.github.junrar.Archive;
import com.github.junrar.Volume;
import com.github.junrar.VolumeManager;

/**
 * @author <a href="http://www.rogiel.com">Rogiel</a>
 * 
 */
public class ByteArrayVolumeManager implements VolumeManager {
    private byte[] bytes;

    public ByteArrayVolumeManager(byte [] bytes) {
        this.bytes = bytes;
    }

    @Override
    public Volume nextArchive(Archive archive, Volume last)
            throws IOException {
        return new ByteArrayVolume( archive, bytes );
    }
}
/*
 * This file is part of seedbox <github.com/seedbox>.
 *
 * seedbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * seedbox is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with seedbox.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.github.junrar.impl;

import java.io.IOException;

import com.github.junrar.Archive;
import com.github.junrar.Volume;
import com.github.junrar.io.IReadOnlyAccess;
import com.github.junrar.io.ReadOnlyAccessByteArray;

/**
 * @author <a href="http://www.rogiel.com">Rogiel</a>
 * 
 */
public class ByteArrayVolume implements Volume {
    private final Archive archive;
    private final byte [] bytes;

    /**
     * @param file
     */
    public ByteArrayVolume(Archive archive, byte [] bytes) {
        this.archive = archive;
        this.bytes = bytes;
    }

    @Override
    public IReadOnlyAccess getReadOnlyAccess() throws IOException {
        return new ReadOnlyAccessByteArray(bytes);
    }

    @Override
    public long getLength() {
        return bytes.length;
    }

    @Override
    public Archive getArchive() {
        return archive;
    }

}