1. ImagesToPdf.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | package app.imgtopdf; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javafx.stage.Stage; import lib.fxapp.FxRunnable; import lib.fxapp.FxWindow; import lib.fxapp.Settings; import lib.java.ThreadPool; public class ImagesToPdf extends Application { FxWindow window; Settings settings; TextField folder; ListView<String> list; int progress; @Override public void start(Stage stage) { initialize(stage); stage.setTitle("Images To PDF Maker V0.0.1"); stage.setScene(new Scene(window.getMainWindow(), 400, 300)); stage.show(); } public static void main(String[] args) { launch(args); } void initialize(Stage stage) { settings = new Settings(); window = new FxWindow(stage, 2); window.addMenu("File"); window.addMenuItem("Exit", runnableExit ); addActionToolbar(0); addFolderToolbar(1); list = window.getDndIconListView(); window.setMainWindow(list); } void addActionToolbar(int index) { window.addToolBarItem( index, window.getButton("Exit", "/res/32/exit.png", runnableExitButton)); window.addToolBarItem( index, window.getButton("Clear", "/res/32/new.png", runnableNewButton)); window.addToolBarItem( index, window.getButton("Make", "/res/32/file_pdf.png", runnablePdfMultiThreadButton)); } TextField addFolderToolbar(int index) { if( settings.get("homeFolder") == null ) { settings.set("homeFolder", window.getHomeFolder()); } window.addToolBarItem( index, window.getLabel( "Output: ")); folder = window.getTextField(settings.get("homeFolder")); window.addToolBarItem( index, folder, true); window.addToolBarItem( index, window.getImageButton("/res/16/folder.png", runnableFolderButton)); return folder; } Runnable runnableExit = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit ?") ) { System.exit(0); } } }; Runnable runnableFolderButton = new Runnable() { @Override public void run() { File file = window.getDirectoryDialog(); if( file != null ) { folder.setText(file.getPath()); } } }; Runnable runnableExitButton = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit ?") ) { System.exit(0); } } }; Runnable runnableNewButton = new Runnable() { @Override public void run() { list.getItems().clear(); list.refresh(); } }; Runnable runnablePdfButton = new Runnable() { @Override public void run() { new Thread() { @Override public void run() { for( int i = 0; i < list.getItems().size(); i++ ) { final int index = i; final String srcFileName = list.getItems().get(i); final File file = new File(srcFileName); MakePdf mp = new MakePdf(); try { mp.makePdf(file, folder.getText(), new FxRunnable() { @Override public void run(Object... object) { long currIndex = (long)object[0]; long totalIndex = (long)object[1]; Platform.runLater( () -> window.setStatusText(file.getName() + " : " + (currIndex+1) + " of " + totalIndex) ); Platform.runLater( () -> list.getItems().set(index, srcFileName + " : " + (currIndex+1) + " of " + totalIndex) ); } }); Platform.runLater( () -> list.getItems().set(index, srcFileName + " .. OK") ); } catch (IOException e) { e.printStackTrace(); Platform.runLater( () -> list.getItems().set(index, srcFileName + " .. Error") ); } } Platform.runLater( () -> window.setStatusText("Completed") ); } }.start(); } }; Runnable runnablePdfMultiThreadButton = new Runnable() { @Override public void run() { new Thread() { @Override public void run() { progress = 0; ThreadPool<Integer> threads = new ThreadPool<Integer>(); threads.createFixedThreadPool(); Platform.runLater( () -> window.setStatusText( "Finish 0 of " + list.getItems().size()) ); for( int i=0; i < list.getItems().size(); i++ ) { final int index = i; final String srcFileName = list.getItems().get(i); final File file = new File(srcFileName); threads.execute(new Runnable() { @Override public void run() { MakePdf mp = new MakePdf(); try { mp.makePdf(file, folder.getText(), new FxRunnable() { @Override public void run(Object... object) { long currIndex = (long)object[0]; long totalIndex = (long)object[1]; Platform.runLater( () -> list.getItems().set(index, srcFileName + " : " + (currIndex+1) + " of " + totalIndex) ); } }); Platform.runLater( () -> list.getItems().set(index, srcFileName + " .. OK") ); } catch (IOException e) { e.printStackTrace(); Platform.runLater( () -> list.getItems().set(index, srcFileName + " .. Error") ); } synchronized(this) { //synchronized(DropGo.class) for static method progress++; Platform.runLater( () -> window.setStatusText( "Finish " + progress + " of " + list.getItems().size()) ); } } }); } try { threads.awaitTermination(1, TimeUnit.HOURS); Platform.runLater( () -> window.setStatusText("Completed") ); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }; } |
2. MakePdf.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package app.imgtopdf; import java.io.File; import java.io.IOException; import lib.fxapp.FxRunnable; import lib.java.FileUtil; import lib.pdf.box.PdfCreate; import lib.tools.ZipUtility; public class MakePdf { public MakePdf( ) { } public boolean makePdf(File srcFile, String outputFolder, FxRunnable runnable) throws IOException { String srcFileName = srcFile.getName(); if( srcFile.isDirectory() ) { dirToPdf(srcFile, outputFolder, runnable); return true; } String srcfileExtension = FileUtil.getFileExtension(srcFileName); if( srcFile.isFile() && srcfileExtension.equalsIgnoreCase("zip") ) { zipToPdf(srcFile, outputFolder, runnable); return true; } return false; } void zipToPdf(File srcFile, String outputFolder, FxRunnable runnable) throws IOException { String pdfName = FileUtil.changeFileExtension( outputFolder + "/" + srcFile.getName(), "pdf" ); PdfCreate pdf = new PdfCreate( pdfName, false); ZipUtility zip = new ZipUtility(); zip.load(srcFile.getPath()); for( int i = 0; i < zip.size(); i++ ) { pdf.newImagePage(zip.getFile(i)); if( runnable != null ) { runnable.run((long)i, (long)zip.size()); } } pdf.save(); } void dirToPdf(File srcFile, String outputFolder, FxRunnable runnable) throws IOException { String pdfName = outputFolder + "/" + srcFile.getName() + ".pdf"; PdfCreate pdf = new PdfCreate( pdfName, false); File[] files = FileUtil.getSortedFileListArray(srcFile.getPath()); for( int i = 0; i < files.length; i++ ) { pdf.newImagePage(files[i].getPath()); if( runnable != null ) { runnable.run((long)i, (long)files.length); } } pdf.save(); } } |
3. ThreadPool.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package lib.java; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class ThreadPool<T> { ExecutorService executorService; CompletionService<T> completionService; public void createFixedThreadPool(int nThreads) { createService(nThreads); } public void createFixedThreadPool() { createService(Runtime.getRuntime().availableProcessors()); } private void createService(int nThreads) { executorService = Executors.newFixedThreadPool(nThreads); completionService = new ExecutorCompletionService<T>(executorService); } public void shutdown() { executorService.shutdown(); } public List<Runnable> shutdownNow() { return executorService.shutdownNow(); } public Boolean awaitTermination( long timeout, TimeUnit unit ) throws InterruptedException { return executorService.awaitTermination(timeout, unit); } public void execute(Runnable task) { executorService.execute(task); } public Future<T> submit(Callable<T> task) { return executorService.submit(task); } public Future<T> submitCompletion(Callable<T> task) { return completionService.submit(task); } } |
4. ZipUtility.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | package lib.tools; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipFile; public class ZipUtility { ZipArchiveOutputStream zos; String zosBaseFolder; String zipFileName; List<FileInfo> fileList; FileInfo currFile; public class FileInfo { public String name; public Long cmp_size; public Long org_size; } public ZipUtility() { } //1 Create ZIP File public void create(String fileName) throws FileNotFoundException { zos = new ZipArchiveOutputStream(new FileOutputStream(fileName)); zos.setEncoding(Charset.defaultCharset().name()); } public void save() throws IOException { if( zos != null ) { zos.close(); } } public void setBaseFolder(String baseFolder) { zosBaseFolder = baseFolder; } public void addFile(File file) throws IOException { if( zos != null ) { String entryName = file.getName();; if( zosBaseFolder != null ) { entryName = file.getPath().substring(zosBaseFolder.length()+1); } zos.putArchiveEntry(new ZipArchiveEntry(entryName)); FileInputStream fis = new FileInputStream(file); byte[] buf = new byte[8192]; int length; while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } fis.close(); zos.closeArchiveEntry(); } } //2. Read ZIP File public void load(String fileName) throws IOException { zipFileName = fileName; makeFileList(); } public int size() { return fileList.size(); } public void getCurrentFileInfo(int i) { if( i < fileList.size() ) { currFile = fileList.get(i); } } public String getName() { return currFile.name; } public long getSize() { return currFile.org_size; } public long getCompressedSize() { return currFile.cmp_size; } public byte[] getCurrentFile() throws IOException { return getFile(currFile); } public byte[] getFile(int index) throws IOException { return getFile(fileList.get(index)); } public void makeFileList() throws IOException { ZipFile zipFile = new ZipFile(zipFileName, Charset.defaultCharset().name()); fileList = new ArrayList<>(); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); while( entries.hasMoreElements() ) { ZipArchiveEntry entry = entries.nextElement(); FileInfo fi = new FileInfo(); fi.name = entry.getName(); fi.cmp_size = entry.getCompressedSize(); fi.org_size = entry.getSize(); fileList.add(fi); } zipFile.close(); } public byte[] getFile(FileInfo fileInfo) throws IOException { ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFileName), Charset.defaultCharset().name(), false); ZipArchiveEntry entry; byte [] fileBuf = null; while ( (entry = zis.getNextZipEntry()) != null ){ if( fileInfo.name.equals(entry.getName()) ) { //System.out.println(entry.getName() + "," + fileInfo.org_size.intValue()); if ( ! entry.isDirectory() ){ fileBuf = new byte[fileInfo.org_size.intValue()]; byte [] buf = new byte[1024 * 8]; int len = 0; int off = 0; while ((len = zis.read(buf)) >= 0 ){ System.arraycopy(buf, 0, fileBuf, off, len); off += len; } } break; } } zis.close(); return fileBuf; } //3. Compress ZIP File public static void zip(File baseDir, List<File> list, File outFile) throws IOException { ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(outFile)); zos.setEncoding(Charset.defaultCharset().name()); for (int i = 0; i < list.size(); i++) { if (list.size() > 0) { String entryName = list.get(i).getPath().substring(baseDir.getPath().length()+1); zos.putArchiveEntry(new ZipArchiveEntry(entryName)); FileInputStream fis = new FileInputStream(list.get(i)); byte[] buf = new byte[8 * 1024]; int length; while ((length = fis.read(buf, 0, buf.length)) >= 0) { zos.write(buf, 0, length); } fis.close(); zos.closeArchiveEntry(); } } zos.close(); } //3. De-compress ZIP File public static void unzip( File inFile, File destDir) throws IOException { ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(inFile), Charset.defaultCharset().name(), false); ZipArchiveEntry entry; destDir.mkdirs(); while ( (entry = zis.getNextZipEntry()) != null ){ String name = entry.getName(); File target = new File (destDir, name); if ( entry.isDirectory() ){ target.mkdirs(); } else { if( ! target.getParentFile().exists() ) { target.getParentFile().mkdirs(); } target.createNewFile(); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); byte [] buf = new byte[1024 * 8]; int len = 0; while ((len = zis.read(buf)) >= 0 ){ bos.write(buf, 0, len); } bos.close(); } } zis.close(); } } |
5. ImagesToPdfTable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | package app.imgtopdf; import java.io.File; import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.stage.Stage; import lib.fxapp.FxRunnable; import lib.fxapp.FxTable; import lib.fxapp.FxWindow; import lib.fxapp.Settings; import lib.java.ThreadPool; public class ImagesToPdfTable extends Application { FxWindow window; Settings settings; TextField folder; FxTable list; int progress; @Override public void start(Stage stage) { initialize(stage); stage.setTitle("Images To PDF Maker V0.0.1"); stage.setScene(new Scene(window.getMainWindow(), 640, 480)); stage.show(); } public static void main(String[] args) { launch(args); } void initialize(Stage stage) { settings = new Settings(); window = new FxWindow(stage, 2); window.addMenu("File"); window.addMenuItem("Exit", runnableExit ); addActionToolbar(0); addFolderToolbar(1); window.setMainWindow(makeTable()); } TableView<?> makeTable() { list = new FxTable(); list.addColumns( new String[] { "Filename", "Status" }); list.setColumnAlign( new Integer[] { -1, 0 } ); list.setColumnWidthFixed( new Integer[] { 500, 140 } ); list.setDragAndDropHandler(new FxRunnable() { @SuppressWarnings("unchecked") @Override public void run(Object... object) { List<File> files = (List<File>) object[0]; for( int i = 0; i < files.size(); i++ ) { list.add( new String[] { files.get(i).getPath(), "Ready" } ); } } }); return list.getTable(); } void addActionToolbar(int index) { window.addToolBarItem( index, window.getButton("Exit", "/res/32/exit.png", runnableExitButton)); window.addToolBarItem( index, window.getButton("Clear", "/res/32/new.png", runnableNewButton)); window.addToolBarItem( index, window.getButton("Make", "/res/32/file_pdf.png", runnablePdfMultiThreadButton)); } TextField addFolderToolbar(int index) { if( settings.get("homeFolder") == null ) { settings.set("homeFolder", window.getHomeFolder()); } window.addToolBarItem( index, window.getLabel( "Output: ")); folder = window.getTextField(settings.get("homeFolder")); window.addToolBarItem( index, folder, true); window.addToolBarItem( index, window.getImageButton("/res/16/folder.png", runnableFolderButton)); return folder; } Runnable runnableExit = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit ?") ) { System.exit(0); } } }; Runnable runnableFolderButton = new Runnable() { @Override public void run() { File file = window.getDirectoryDialog(); if( file != null ) { folder.setText(file.getPath()); } } }; Runnable runnableExitButton = new Runnable() { @Override public void run() { if( window.yesno("Exit", "Do you want exit ?") ) { System.exit(0); } } }; Runnable runnableNewButton = new Runnable() { @Override public void run() { list.clear(); list.refresh(); } }; Runnable runnablePdfButton = new Runnable() { @Override public void run() { new Thread() { @Override public void run() { for( int i = 0; i < list.size(); i++ ) { final int index = i; final String srcFileName = list.get(i,0); final File file = new File(srcFileName); MakePdf mp = new MakePdf(); try { mp.makePdf(file, folder.getText(), new FxRunnable() { @Override public void run(Object... object) { long currIndex = (long)object[0]; long totalIndex = (long)object[1]; Platform.runLater( () -> window.setStatusText(file.getName() + " : " + (currIndex+1) + " of " + totalIndex) ); Platform.runLater( () -> list.set(index, 1, "" + (currIndex+1) + " of " + totalIndex) ); } }); Platform.runLater( () -> list.set(index, 1, "OK") ); } catch (IOException e) { e.printStackTrace(); Platform.runLater( () -> list.set(index, 1, "Error") ); } } Platform.runLater( () -> window.setStatusText("Completed") ); } }.start(); } }; Runnable runnablePdfMultiThreadButton = new Runnable() { @Override public void run() { new Thread() { @Override public void run() { progress = 0; ThreadPool<Integer> threads = new ThreadPool<Integer>(); threads.createFixedThreadPool(); Platform.runLater( () -> window.setStatusText( "Finish 0 of " + list.size()) ); for( int i=0; i < list.size(); i++ ) { final int index = i; final String srcFileName = list.get(i,0); final File file = new File(srcFileName); threads.execute(new Runnable() { @Override public void run() { MakePdf mp = new MakePdf(); try { mp.makePdf(file, folder.getText(), new FxRunnable() { @Override public void run(Object... object) { long currIndex = (long)object[0]; long totalIndex = (long)object[1]; Platform.runLater( () -> list.set(index, 1, "" + (currIndex+1) + " of " + totalIndex) ); } }); Platform.runLater( () -> list.set(index, 1, "OK") ); } catch (IOException e) { e.printStackTrace(); Platform.runLater( () -> list.set(index, 1, "Error") ); } synchronized(this) { //synchronized(DropGo.class) for static method progress++; Platform.runLater( () -> window.setStatusText( "Finish " + progress + " of " + list.size()) ); } } }); } try { threads.awaitTermination(1, TimeUnit.HOURS); Platform.runLater( () -> window.setStatusText("Completed") ); } catch (InterruptedException e) { e.printStackTrace(); } } }.start(); } }; } |
댓글 없음:
댓글 쓰기