Skip to content

Instantly share code, notes, and snippets.

@bobbyjam99-zz
Created April 8, 2014 09:13
Show Gist options
  • Save bobbyjam99-zz/10103580 to your computer and use it in GitHub Desktop.
Save bobbyjam99-zz/10103580 to your computer and use it in GitHub Desktop.
POIでファイルにパスワード
package sample.poi;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class App {
public static void main(String[] args) throws Exception {
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");
OPCPackage opc = OPCPackage.open("/path/to/file");
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream fos = new FileOutputStream("/path/to/encfile");
fs.writeFilesystem(fos);
fos.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment