Created
November 20, 2012 02:03
-
-
Save thara/4115486 to your computer and use it in GitHub Desktop.
Zipファイルに圧縮する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 指定されたファイルをZip形式で圧縮し、同じディレクトリに作成する。 | |
* | |
* @param zipFile 作成先のZIPファイルを示す{@link File}オブジェクト | |
* @param srcFile 圧縮対象の元ファイルを示す{@link File}オブジェクト | |
* @throws IOException ファイルIOで例外が発生した場合 | |
*/ | |
void makeZip(File zipFile, File srcFile) throws IOException { | |
final int bufferSize = 256; | |
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); | |
try { | |
// ファイル名のみの指定は、出力先と同じディレクトリに圧縮ファイルが作成される。 | |
ZipEntry ze = new ZipEntry(srcFile.getName()); | |
zos.putNextEntry(ze); | |
InputStream is = new BufferedInputStream(new FileInputStream(srcFile)); | |
byte[] buf = new byte[bufferSize]; | |
int len = 0; | |
try { | |
while ((len = is.read(buf)) != -1) { | |
zos.write(buf, 0, len); | |
} | |
} finally { | |
is.close(); | |
} | |
} finally { | |
zos.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment