Here you can find the source of zip(File theFileToZip)
Parameter | Description |
---|---|
theFileToZip | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void zip(File theFileToZip) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) JavaPEG developers// w w w . j a va 2 s . c o m * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** * @param theFileToZip * @throws IOException */ public static void zip(File theFileToZip) throws IOException { byte[] buf = new byte[8096]; File zipFile = new File(theFileToZip.getParent(), theFileToZip.getName() + ".zip"); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { try (FileInputStream fis = new FileInputStream(theFileToZip)) { zos.putNextEntry(new ZipEntry(theFileToZip.getName())); int lenght; while ((lenght = fis.read(buf)) > 0) { zos.write(buf, 0, lenght); } zos.closeEntry(); } } } }