/* * Copyright (C) 2014 TU Darmstadt, Hessen, Germany. * Department of Computer Science Databases and Distributed Systems * * 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 3 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/>. */ package de.tudarmstadt.dvs.myhealthassistant.myhealthhub.commontools; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import android.util.Log; public class Compress { private static final int BUFFER = 2048; private File file; private File zipFile; public Compress(File file, File zipFile) { this.file = file; this.zipFile = zipFile; } public void zip() { try { BufferedInputStream origin = null; Log.d("Compress", "-- " + zipFile.getAbsolutePath()); FileOutputStream dest = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream( dest)); byte data[] = new byte[BUFFER]; Log.v("Compress", "Adding: " + file.getName()+" isDirectory: "+file.isDirectory()+ " canRead: "+file.canRead()); FileInputStream fi = new FileInputStream(file); origin = new BufferedInputStream(fi, BUFFER); ZipEntry entry = new ZipEntry(file.getName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } origin.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }