Search in sources :

Example 16 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project Anki-Android by Ramblurr.

the class Media method zipAdded.

// Media syncing - bundling zip files to send to server
// Because there's no standard filename encoding for zips, and because not
// all zip clients support retrieving mtime, we store the files as ascii
// and place a json file in the zip with the necessary information.
// /////////////////////////////////////////////////////
/**
     * Add files to a zip until over SYNC_ZIP_SIZE. Return zip data.
     * 
     * @return Returns a tuple with two objects. The first one is the zip file contents, the second a list with the
     *         filenames of the files inside the zip.
     */
public Pair<File, List<String>> zipAdded() {
    File f = new File(mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncToServer.zip"));
    String sql = "select fname from log where type = " + Integer.toString(MEDIA_ADD);
    List<String> filenames = mMediaDb.queryColumn(String.class, sql, 0);
    List<String> fnames = new ArrayList<String>();
    try {
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
        zos.setLevel(8);
        JSONObject files = new JSONObject();
        int cnt = 0;
        long sz = 0;
        byte[] buffer = new byte[2048];
        boolean finished = true;
        for (String fname : filenames) {
            fnames.add(fname);
            File file = new File(getDir(), fname);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), 2048);
            ZipEntry entry = new ZipEntry(Integer.toString(cnt));
            zos.putNextEntry(entry);
            int count = 0;
            while ((count = bis.read(buffer, 0, 2048)) != -1) {
                zos.write(buffer, 0, count);
            }
            zos.closeEntry();
            bis.close();
            files.put(Integer.toString(cnt), fname);
            sz += file.length();
            if (sz > SYNC_ZIP_SIZE) {
                finished = false;
                break;
            }
            cnt += 1;
        }
        if (finished) {
            zos.putNextEntry(new ZipEntry("_finished"));
            zos.closeEntry();
        }
        zos.putNextEntry(new ZipEntry("_meta"));
        zos.write(Utils.jsonToString(files).getBytes());
        zos.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    return new Pair<File, List<String>>(f, fnames);
}
Also used : ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JSONObject(org.json.JSONObject) BufferedInputStream(java.io.BufferedInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Pair(com.ichi2.anki.Pair)

Example 17 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project SKMCLauncher by SKCraft.

the class JarBuilder method rebuild.

/**
     * Rebuild the target JAR file to contain the list of included JAR files.
     *
     * @throws IOException on I/O error
     */
public void rebuild() throws IOException {
    Set<String> existing = new HashSet<String>();
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ZipOutputStream zos = null;
    log.log(Level.INFO, "Rebuilding {0}", getTarget().getAbsolutePath());
    try {
        fos = new FileOutputStream(getTarget());
        bos = new BufferedOutputStream(fos);
        zos = new ZipOutputStream(bos);
        // Merge in the patch .jars in reverse order
        ListIterator<String> it = getIncluded().listIterator(getIncluded().size());
        while (it.hasPrevious()) {
            File file = new File(dir, it.previous());
            if (file.exists()) {
                try {
                    mergeInto(zos, file, existing);
                } catch (IOException e) {
                    log.log(Level.WARNING, "Failed to merge in " + file.getAbsolutePath(), e);
                }
            } else {
                log.log(Level.WARNING, "Can't merge in " + file.getAbsolutePath());
            }
        }
        // Now merge in the base .jar
        mergeInto(zos, getSource(), existing);
        setDirty(false);
    } finally {
        closeQuietly(zos);
        closeQuietly(bos);
        closeQuietly(fos);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ToString(lombok.ToString)

Example 18 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project sonarqube by SonarSource.

the class ZipUtils method zipDir.

public static void zipDir(File dir, File zip) throws IOException {
    OutputStream out = null;
    ZipOutputStream zout = null;
    try {
        out = FileUtils.openOutputStream(zip);
        zout = new ZipOutputStream(out);
        doZipDir(dir, zout);
    } finally {
        IOUtils.closeQuietly(zout);
        IOUtils.closeQuietly(out);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 19 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project tinker by Tencent.

the class DexDiffPatchInternal method patchDexFile.

/**
     * Generate patched dex file (May wrapped it by a jar if needed.)
     * @param baseApk
     *   OldApk.
     * @param patchPkg
     *   Patch package, it is also a zip file.
     * @param oldDexEntry
     *   ZipEntry of old dex.
     * @param patchFileEntry
     *   ZipEntry of patch file. (also ends with .dex) This could be null.
     * @param patchInfo
     *   Parsed patch info from package-meta.txt
     * @param patchedDexFile
     *   Patched dex file, may be a jar.
     *
     * <b>Notice: patchFileEntry and smallPatchInfoFile cannot both be null.</b>
     *
     * @throws IOException
     */
private static void patchDexFile(ZipFile baseApk, ZipFile patchPkg, ZipEntry oldDexEntry, ZipEntry patchFileEntry, ShareDexDiffPatchInfo patchInfo, File patchedDexFile) throws IOException {
    InputStream oldDexStream = null;
    InputStream patchFileStream = null;
    try {
        oldDexStream = new BufferedInputStream(baseApk.getInputStream(oldDexEntry));
        patchFileStream = (patchFileEntry != null ? new BufferedInputStream(patchPkg.getInputStream(patchFileEntry)) : null);
        final boolean isRawDexFile = SharePatchFileUtil.isRawDexFile(patchInfo.rawName);
        if (!isRawDexFile || patchInfo.isJarMode) {
            ZipOutputStream zos = null;
            try {
                zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(patchedDexFile)));
                zos.putNextEntry(new ZipEntry(ShareConstants.DEX_IN_JAR));
                // Old dex is not a raw dex file.
                if (!isRawDexFile) {
                    ZipInputStream zis = null;
                    try {
                        zis = new ZipInputStream(oldDexStream);
                        ZipEntry entry;
                        while ((entry = zis.getNextEntry()) != null) {
                            if (ShareConstants.DEX_IN_JAR.equals(entry.getName()))
                                break;
                        }
                        if (entry == null) {
                            throw new TinkerRuntimeException("can't recognize zip dex format file:" + patchedDexFile.getAbsolutePath());
                        }
                        new DexPatchApplier(zis, patchFileStream).executeAndSaveTo(zos);
                    } finally {
                        SharePatchFileUtil.closeQuietly(zis);
                    }
                } else {
                    new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(zos);
                }
                zos.closeEntry();
            } finally {
                SharePatchFileUtil.closeQuietly(zos);
            }
        } else {
            new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(patchedDexFile);
        }
    } finally {
        SharePatchFileUtil.closeQuietly(oldDexStream);
        SharePatchFileUtil.closeQuietly(patchFileStream);
    }
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) ZipInputStream(java.util.zip.ZipInputStream) DexPatchApplier(com.tencent.tinker.commons.dexpatcher.DexPatchApplier) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) BufferedOutputStream(java.io.BufferedOutputStream)

Example 20 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project tinker by Tencent.

the class DexDiffPatchInternal method extractDexToJar.

/**
     * repack dex to jar
     *
     * @param zipFile
     * @param entryFile
     * @param extractTo
     * @param targetMd5
     * @return boolean
     * @throws IOException
     */
private static boolean extractDexToJar(ZipFile zipFile, ZipEntry entryFile, File extractTo, String targetMd5) throws IOException {
    int numAttempts = 0;
    boolean isExtractionSuccessful = false;
    while (numAttempts < MAX_EXTRACT_ATTEMPTS && !isExtractionSuccessful) {
        numAttempts++;
        FileOutputStream fos = new FileOutputStream(extractTo);
        InputStream in = zipFile.getInputStream(entryFile);
        ZipOutputStream zos = null;
        BufferedInputStream bis = null;
        TinkerLog.i(TAG, "try Extracting " + extractTo.getPath());
        try {
            zos = new ZipOutputStream(new BufferedOutputStream(fos));
            bis = new BufferedInputStream(in);
            byte[] buffer = new byte[ShareConstants.BUFFER_SIZE];
            ZipEntry entry = new ZipEntry(ShareConstants.DEX_IN_JAR);
            zos.putNextEntry(entry);
            int length = bis.read(buffer);
            while (length != -1) {
                zos.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            zos.closeEntry();
        } finally {
            SharePatchFileUtil.closeQuietly(bis);
            SharePatchFileUtil.closeQuietly(zos);
        }
        isExtractionSuccessful = SharePatchFileUtil.verifyDexFileMd5(extractTo, targetMd5);
        TinkerLog.i(TAG, "isExtractionSuccessful: %b", isExtractionSuccessful);
        if (!isExtractionSuccessful) {
            extractTo.delete();
            if (extractTo.exists()) {
                TinkerLog.e(TAG, "Failed to delete corrupted dex " + extractTo.getPath());
            }
        }
    }
    return isExtractionSuccessful;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)463 ZipEntry (java.util.zip.ZipEntry)308 FileOutputStream (java.io.FileOutputStream)216 File (java.io.File)181 IOException (java.io.IOException)118 ZipFile (java.util.zip.ZipFile)104 BufferedOutputStream (java.io.BufferedOutputStream)90 ByteArrayOutputStream (java.io.ByteArrayOutputStream)83 FileInputStream (java.io.FileInputStream)82 Test (org.junit.Test)68 InputStream (java.io.InputStream)61 ZipInputStream (java.util.zip.ZipInputStream)44 ByteArrayInputStream (java.io.ByteArrayInputStream)39 BufferedInputStream (java.io.BufferedInputStream)28 OutputStream (java.io.OutputStream)26 Path (java.nio.file.Path)22 ArrayList (java.util.ArrayList)19 FileNotFoundException (java.io.FileNotFoundException)18 HashMap (java.util.HashMap)17 CRC32 (java.util.zip.CRC32)13