Java ZipOutputStream Write zipOneFile(File file, ZipOutputStream out, String relativePath)

Here you can find the source of zipOneFile(File file, ZipOutputStream out, String relativePath)

Description

Introduce un fichero en el stream de zip.

License

Open Source License

Parameter

Parameter Description
file Fichero que queremos introducir en el stream
out stream de zip donde metemos el fichero que queremos comprimir

Declaration

static private void zipOneFile(File file, ZipOutputStream out, String relativePath)
        throws IOException, FileNotFoundException 

Method Source Code

//package com.java2s;
/*/*from   ww w  . j  a  va2 s.  co  m*/
 * ISABEL: A group collaboration tool for the Internet
 * Copyright (C) 2009 Agora System S.A.
 * 
 * This file is part of Isabel.
 * 
 * Isabel is free software: you can redistribute it and/or modify
 * it under the terms of the Affero GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Isabel 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
 * Affero GNU General Public License for more details.
 * 
 * You should have received a copy of the Affero GNU General Public License
 * along with Isabel.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.zip.*;
import java.io.*;

public class Main {
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");
    public static final String XLIM_LOGS_WORK_DIR = System.getProperty("user.home") + FILE_SEPARATOR + ".xlim"
            + FILE_SEPARATOR + "logs" + FILE_SEPARATOR;

    /**
     * Introduce un fichero en el stream de zip.
     * @param file Fichero que queremos introducir en el stream
     * @param out stream de zip donde metemos el fichero que queremos comprimir
     * @Author lailoken
     */
    static private void zipOneFile(File file, ZipOutputStream out, String relativePath)
            throws IOException, FileNotFoundException {
        // Donde mandamos los mensajes de trazas: 
        PrintWriter outTraceFile = new PrintWriter(
                new BufferedWriter(new FileWriter(XLIM_LOGS_WORK_DIR + FILE_SEPARATOR + "zip.log", true)), true);
        if (file.isDirectory()) {
            outTraceFile.println("Zipping..." + file.getPath());
            //Create an array with all of the files and subdirectories
            //of the current directory.
            String[] fileNamesInDir = file.list();
            if (fileNamesInDir != null) {
                //Recursively add each array entry to make sure that we get 
                //subdirectories as well as normal files in the directory.
                for (int i = 0; i < fileNamesInDir.length; i++) {
                    zipOneFile(new File(file, fileNamesInDir[i]), out, relativePath);
                }
            }
        }
        //Otherwise, a file so add it as an entry to the Zip file.
        else {
            //----- For each file --
            outTraceFile.println("Zipping..." + file.getPath());
            byte[] buf = new byte[1024];
            FileInputStream in = new FileInputStream(file);
            // Add ZIP entry to output stream.
            // Sacamos el nombre de la entry de quitarle al nombre del fichero su relativePath:
            outTraceFile.println("filePath: " + file.getPath() + "; relativePath: " + relativePath);
            String entryName;
            // Para generar el entryName hay que asegurarnos  de que le hemos dejado el path relativo pedido
            entryName = file.getPath().substring(relativePath.length(), file.getPath().length());
            // ...y de que los separadores de directorio son /
            outTraceFile.println("EntryName: " + entryName);
            entryName = entryName.replaceAll("\\\\", "/");
            outTraceFile.println("EntryName: " + entryName);
            out.putNextEntry(new ZipEntry(entryName));
            // Transfer bytes from the file to the ZIP file
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            // Complete the entry
            out.closeEntry();
            in.close();
            //----------------------
        }
    }
}

Related

  1. zipFile(ZipOutputStream zout, File file, int rootLength)
  2. zipInternal(String path, File file, ZipOutputStream os)
  3. zipInternal(String path, File file, ZipOutputStream os, boolean justFolders, boolean skipManifest)
  4. zipIt(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc)
  5. zipOneFile(File file, String base, ZipOutputStream zout)
  6. zipOneFile(String prefix, File file, ZipOutputStream zos)
  7. zipRecurse(File file, int prefixLength, ZipOutputStream zos)
  8. zipRecursively(String path, File dir, ZipOutputStream out)
  9. zipStoreBuffer(ZipOutputStream zip, String name, byte[] dataBuffer)