アプリ版:「スタンプのみでお礼する」機能のリリースについて

お世話になります。

開発環境は以下のとおりです。
開発ツール NetBeans IDE 6.5
JDK 1.6.0_11
OS Windows XP SP2
ant.jar version 1.7.1

はじめ、JDK付属の「java.util.zip.ZipOutputStream」,「java.util.zip.Entry」を用いて、
ZIPファイルを作成していたのですが、
日本語名のファイルを圧縮すると文字化けしてしまい、
調べてみるとJakart Ant内のant.jarの「org.apache.tools.zip.ZipOutputStream」,「org.apache.tools.zip.Entry」を使って
import宣言を変えてsetEncodingしてしまえば簡単に出来るという情報がありました。

早速やってみたのですが、
1.ZIPファイルは作成される。このときファイルサイズがjava.util.zip.*を使ったときと同じ
2.LhaplusやWinRARといった解凍ツールを使って解凍するとエラーが
発生して中身がない
3.日本語が含まれないファイルを圧縮・解凍しても同じ現象
4.java.util.zip.* を使ったときは正常に圧縮・解凍できる(文字化けはしていますが・・・)
といった状況になってしまいました。

ロジックを何度も見直し、ZipOutputStreamのflush、closeもしっかり行っていますし、CRC32の設定やファイルサイズの設定も手順どおりにやっているつもりです。

どなたかご存知のかたがいらっしゃいましたらご教授の程、よろしくお願いいたします。

A 回答 (1件)

具体的なコードが無いので何とも言いようが無いのですが、


以下のコードで試してみてください。
私の環境では、問題なく動作しているクラスです。

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


/**
*
* 複数のファイル及び、サブディレクトリを含めたディレクトリを
* zip圧縮・解凍する為のクラスです。
*
* @author HarukaV49
*
*/
public class FileZip {

private static final int EOF = -1;
private static final int ZIP_BUFF_SIZE = 1024;

/**
* デフォルト(ファイル名解析)エンコーダを使用してファイル及びディレクトリをZIP圧縮します。
* @param zipFilename 作成されるZIPファイル名
* @param targetFiles 圧縮対象のファイル及びディレクトリ名列
* @return 作成されたZIPファイル
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File zip(String zipFilename, String... targetFiles) throws ZipException, FileNotFoundException, IOException {
return zip(zipFilename, targetFiles, null);
}
/**
* ファイル及びディレクトリをZIP圧縮します。
* @param zipFilename 作成されるZIPファイル名
* @param targetFiles targetFiles 圧縮対象のファイル及びディレクトリ名配列
* @param encoding ファイル名解析エンコーダ名(<A HREF="http://java.sun.com/javase/6/docs/technotes/guid …一覧</A>)
* @return 作成されたZIPファイル
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File zip(String zipFilename, String[] targetFiles, String encoding) throws ZipException, FileNotFoundException, IOException {
int n = targetFiles.length;
File[] files = new File[n];
for(int i=0; i<n; i++) {
files[i] = new File( targetFiles[i] );
}
return zip( new File(zipFilename), files, encoding);
}
/**
* ファイル及びディレクトリをZIP圧縮します。
* @param zipFile 作成されるZIPファイル
* @param targetFiles targetFiles 圧縮対象のファイル及びディレクトリ配列
* @param encoding ファイル名解析エンコーダ名(<A HREF="http://java.sun.com/javase/6/docs/technotes/guid …一覧</A>)
* @return 作成されたZIPファイル
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File zip(File zipFile, File[] targetFiles, String encoding) throws ZipException, FileNotFoundException, IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
out.setEncoding(encoding);
for(int i=0; i<targetFiles.length; i++) {
int deleteLength = targetFiles[i].getPath().length() - targetFiles[i].getName().length();
zip(out, targetFiles[i], deleteLength);
}
out.close();
return zipFile;
}
private static void zip(ZipOutputStream out, File targetFile, int deleteLength) throws IOException {
if( targetFile.isDirectory() ) {
File[] files = targetFile.listFiles();
for(int i=0; i<files.length; i++) {
zip(out, files[i], deleteLength);
}
} else {
ZipEntry target = new ZipEntry( targetFile.getPath().substring(deleteLength) );
out.putNextEntry(target);
byte buf[] = new byte[ZIP_BUFF_SIZE];
int count;
BufferedInputStream in = new BufferedInputStream( new FileInputStream(targetFile) );
while( (count = in.read(buf, 0, ZIP_BUFF_SIZE)) != EOF ) {
out.write(buf, 0, count);
}
in.close();
out.closeEntry();
}
}

/**
* デフォルト(ファイル名解析)エンコーダを使用してZIPファイルを解凍します。
* @param filename ZIPファイル名
* @param outDir 解凍先ディレクトリ名
* @return 解凍されたファイルまたはディレクトリ
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File unzip(String filename, String outDir) throws ZipException, FileNotFoundException, IOException {
return unzip(filename, outDir, null);
}
/**
* ZIPファイルを解凍します。
* @param filename ZIPファイル名
* @param outDir 解凍先ディレクトリ名
* @param encoding ファイル名解析エンコーダ名(<A HREF="http://java.sun.com/javase/6/docs/technotes/guid …一覧</A>)
* @return 解凍されたファイルまたはディレクトリ
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File unzip(String filename, String outDir, String encoding) throws ZipException, FileNotFoundException, IOException {
return unzip( new File(filename), new File(outDir), encoding );
}
/**
* ZIPファイルを解凍します。
* @param filename ZIPファイル
* @param outDir 解凍先ディレクトリ
* @param encoding ファイル名解析エンコーダ名(<A HREF="http://java.sun.com/javase/6/docs/technotes/guid …一覧</A>)
* @return 解凍されたファイルまたはディレクトリ
* @throws ZipException ZIP例外
* @throws FileNotFoundException ファイル例外
* @throws IOException IO例外
*/
public static File unzip(File filename, File outDir, String encoding) throws ZipException, FileNotFoundException, IOException {
ZipFile zipFile = new ZipFile(filename, encoding);
@SuppressWarnings("unchecked")
Enumeration<ZipEntry> enumeration = zipFile.getEntries();
while( enumeration.hasMoreElements() ) {
ZipEntry entry = enumeration.nextElement();
if( entry.isDirectory() ) {
new File(entry.getName()).mkdirs();
} else {
File file = new File(outDir, entry.getName());
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
InputStream in = zipFile.getInputStream(entry);
byte[] buf = new byte[ZIP_BUFF_SIZE];
int size = 0;
while( (size = in.read(buf)) != EOF ) {
out.write(buf, 0, size);
}
out.close();
in.close();
}
}
zipFile.close();
return outDir;
}

}
    • good
    • 0
この回答へのお礼

ありがとうございます!

HarukaV49さんのソースと私のソースの違いは
私が処理を書き過ぎだったのかもしれません。
随分簡単にお書きになるなと思ったのですが、
同じようにしてみたら出来ました!

ありがとうございます!

お礼日時:2009/07/27 09:36

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!