Search in sources :

Example 16 with ZipException

use of net.lingala.zip4j.exception.ZipException in project Cartographer by johannesmols.

the class Unzipper method unzip.

public boolean unzip(Uri file) {
    try {
        String filePath = getPath(context, file);
        // Build destination path to be in the same directory as the zip file, by taking the zip file path and removing the last part
        if (filePath != null) {
            String[] destinationArray = filePath.split("(?=/)");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < destinationArray.length - 1; i++) {
                // loop until the second last item, so the file name is omitted
                stringBuilder.append(destinationArray[i]);
            }
            stringBuilder.append("/");
            String destination = stringBuilder.toString();
            // Check for read file permission
            Permissions permissions = new Permissions(context);
            if (!permissions.checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                permissions.askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, Permissions.RequestCodes.WRITE_EXTERNAL_STORAGE);
                if (!permissions.checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    Toasty.error(context, context.getString(R.string.toast_permission_denied), Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
            ZipFile zipFile = new ZipFile(filePath);
            if (zipFile.isEncrypted()) {
                throw new ZipException("File is encrypted");
            }
            zipFile.extractAll(destination);
        } else {
            Toasty.error(context, context.getString(R.string.toast_zip_error), Toast.LENGTH_LONG).show();
            return false;
        }
    } catch (ZipException e) {
        e.printStackTrace();
    }
    return true;
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) ZipException(net.lingala.zip4j.exception.ZipException)

Example 17 with ZipException

use of net.lingala.zip4j.exception.ZipException in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldModifyCommands method start.

@Subcommand("start")
@CommandPermission("%admin")
@Syntax("<world> - the name of the map you want to modify")
public void start(@Nonnull User user, @Nonnull String world) {
    if (editor != null) {
        Lang.msg(user, LangKey.WORLD_CREATOR_IN_USE, editor.getDisplayName());
        return;
    }
    editor = user;
    // load data
    Optional<Map> o = worldHandler.getMap(world);
    this.map = o.orElseGet(() -> worldHandler.loadMap(world));
    // load world
    map.load(editor.getUuid(), map.getWorldName());
    File file = new File(worldHandler.getWorldContainer(), map.getLoadedName(editor.getUuid()));
    try {
        ZipFile zip = new ZipFile(new File(worldHandler.getWorldsFolder(), map.getWorldName() + ".zip"));
        zip.extractAll(file.getAbsolutePath());
        FileUtils.delete(new File(file, "uid.dat"));
    } catch (ZipException e) {
        throw new WorldException("Could not unzip world " + map.getInfo().getName() + ".", e);
    }
    worldHandler.loadLocalWorld(map.getLoadedName(editor.getUuid()));
    // tp
    user.getPlayer().teleport(map.getCenter().toLocation(map.getLoadedName(user.getUuid())));
    if (gameHandler.getDefaultGame().isParticipating(editor.getUuid())) {
        gameHandler.getDefaultGame().leave(editor);
    }
    game = gameHandler.startGame(EditModeGame.GAMEMODE);
    game.getActivePhase().getNextPhase().getFeature(SpawnFeature.class).addSpawn(map.getCenter());
    game.getActivePhase().getNextPhase().getFeature(MapFeature.class).setMap(map);
    map.load(game.getUuid(), map.getWorldName());
    game.join(editor);
    game.endPhase();
    Lang.msg(user, LangKey.WORLD_MODIFY_START);
// TODO use inventory for world creator
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) WorldException(com.voxelgameslib.voxelgameslib.exception.WorldException) MapFeature(com.voxelgameslib.voxelgameslib.feature.features.MapFeature) ZipException(net.lingala.zip4j.exception.ZipException) Map(com.voxelgameslib.voxelgameslib.map.Map) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) SpawnFeature(com.voxelgameslib.voxelgameslib.feature.features.SpawnFeature) Subcommand(co.aikar.commands.annotation.Subcommand) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 18 with ZipException

use of net.lingala.zip4j.exception.ZipException in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldHandler method loadWorld.

/**
 * Loads a world. Needs to copy the file from the repo, unzip it and let the implementation load it <br><b>Always
 * needs to call super! Super needs to be called first (because it copies the world)</b>
 *
 * @param map    the map that should be loaded
 * @param gameid the id of the game this map belongs to
 * @return the loaded world
 * @throws WorldException something goes wrong
 */
@Nonnull
public World loadWorld(@Nonnull Map map, @Nonnull UUID gameid, boolean replaceMarkers) {
    map.load(gameid, "TEMP_" + map.getWorldName() + "_" + gameid.toString().split("-")[0]);
    log.finer("Loading map " + map.getInfo().getName() + " as " + map.getLoadedName(gameid));
    File file = new File(worldContainer, map.getLoadedName(gameid));
    try {
        ZipFile zip = new ZipFile(new File(worldsFolder, map.getWorldName() + ".zip"));
        zip.extractAll(file.getAbsolutePath());
        FileUtils.delete(new File(file, "uid.dat"));
    } catch (ZipException e) {
        throw new WorldException("Could not unzip world " + map.getInfo().getName() + ".", e);
    }
    World world = loadLocalWorld(map.getLoadedName(gameid));
    if (replaceMarkers) {
        replaceMarkers(world, map);
    }
    // load chunks based on markers
    int i = 0;
    int a = 0;
    for (Marker m : map.getMarkers()) {
        Location l = m.getLoc().toLocation(world.getName());
        if (!l.getChunk().isLoaded() || !l.getWorld().isChunkLoaded(l.getChunk())) {
            l.getChunk().load();
            l.getWorld().loadChunk(l.getChunk());
            i++;
        } else {
            a++;
        }
    }
    log.finer("Loaded " + i + " chunks and ignored " + a + " already loaded");
    return world;
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) WorldException(com.voxelgameslib.voxelgameslib.exception.WorldException) ZipException(net.lingala.zip4j.exception.ZipException) Marker(com.voxelgameslib.voxelgameslib.map.Marker) World(org.bukkit.World) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) Location(org.bukkit.Location) Nonnull(javax.annotation.Nonnull)

Example 19 with ZipException

use of net.lingala.zip4j.exception.ZipException in project ArachneCentralAPI by OHDSI.

the class AnalysisHelper method compressAndSplit.

public void compressAndSplit(ArrayList<File> files, File zipArchive) {
    try {
        ZipFile zipFile = new ZipFile(zipArchive.getAbsoluteFile());
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        zipFile.createSplitZipFile(files, parameters, true, maximumSize);
    } catch (ZipException ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new ConverterRuntimeException(ex.getMessage());
    }
}
Also used : ZipFile(net.lingala.zip4j.ZipFile) ZipException(net.lingala.zip4j.exception.ZipException) ConverterRuntimeException(com.odysseusinc.arachne.portal.exception.ConverterRuntimeException) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Example 20 with ZipException

use of net.lingala.zip4j.exception.ZipException in project Auto.js by hyb1996.

the class AndroidClassLoader method loadJar.

public void loadJar(File jar) throws IOException {
    try {
        final ZipFile zipFile = new ZipFile(classFile);
        final ZipFile jarFile = new ZipFile(jar);
        // noinspection unchecked
        for (FileHeader header : (List<FileHeader>) jarFile.getFileHeaders()) {
            if (!header.isDirectory()) {
                final ZipParameters parameters = new ZipParameters();
                parameters.setFileNameInZip(header.getFileName());
                parameters.setSourceExternalStream(true);
                zipFile.addStream(jarFile.getInputStream(header), parameters);
            }
        }
        dexJar();
    } catch (ZipException e) {
        IOException exception = new IOException();
        // noinspection UnnecessaryInitCause
        exception.initCause(e);
        throw exception;
    }
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) List(java.util.List) ArrayList(java.util.ArrayList) ZipException(net.lingala.zip4j.exception.ZipException) IOException(java.io.IOException) FileHeader(net.lingala.zip4j.model.FileHeader) ZipParameters(net.lingala.zip4j.model.ZipParameters)

Aggregations

ZipException (net.lingala.zip4j.exception.ZipException)23 ZipFile (net.lingala.zip4j.core.ZipFile)15 File (java.io.File)13 ZipParameters (net.lingala.zip4j.model.ZipParameters)9 FileHeader (net.lingala.zip4j.model.FileHeader)8 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ZipFile (net.lingala.zip4j.ZipFile)3 WorldException (com.voxelgameslib.voxelgameslib.exception.WorldException)2 BinaryContent (ddf.catalog.data.BinaryContent)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 ZipFile (java.util.zip.ZipFile)2 HeaderReader (net.lingala.zip4j.core.HeaderReader)2 LocalFileHeader (net.lingala.zip4j.model.LocalFileHeader)2 ZipModel (net.lingala.zip4j.model.ZipModel)2 UnzipEngine (net.lingala.zip4j.unzip.UnzipEngine)2 FileMeta (org.molgenis.data.file.model.FileMeta)2 Content (v7db.files.spi.Content)2