Skip to content

Commit 81f1b36

Browse files
author
SeTSeR
committed
Added saving to zip
1 parent c86d607 commit 81f1b36

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

src/com/temporaryteam/noticeditor/view/Main.fxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<MenuItem fx:id="openItem" mnemonicParsing="false" onAction="#handleMenu" text="Open" />
1818
<MenuItem fx:id="saveItem" mnemonicParsing="false" onAction="#handleMenu" text="Save" />
1919
<MenuItem fx:id="saveAsItem" mnemonicParsing="false" onAction="#handleMenu" text="Save As" />
20+
<MenuItem fx:id="zipItem" mnemonicParsing="false" onAction="#handleMenu" text="Save in zip" />
2021
<MenuItem fx:id="exportHTMLItem" mnemonicParsing="false" onAction="#handleMenu" text="Export to HTML" />
2122
<MenuItem fx:id="exitItem" mnemonicParsing="false" onAction="#handleMenu" text="Close" />
2223
</items>

src/com/temporaryteam/noticeditor/view/NoticeController.java

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55

66
import org.pegdown.PegDownProcessor;
77

8+
import java.net.URI;
9+
import java.io.Closeable;
810
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.FileOutputStream;
913
import java.io.FileWriter;
1014
import java.io.IOException;
11-
import java.util.Scanner;
15+
import java.io.InputStream;
16+
import java.io.OutputStream;
1217
import java.util.ArrayList;
18+
import java.util.Deque;
19+
import java.util.LinkedList;
20+
import java.util.Scanner;
21+
import java.util.zip.ZipEntry;
22+
import java.util.zip.ZipOutputStream;
1323

1424
import javafx.util.Callback;
1525
import javafx.application.Platform;
@@ -53,6 +63,9 @@ public class NoticeController {
5363
@FXML
5464
private MenuItem saveAsItem;
5565

66+
@FXML
67+
private MenuItem zipItem;
68+
5669
@FXML
5770
private MenuItem exportHTMLItem;
5871

@@ -97,6 +110,48 @@ public void setCurrentTreeItem(NoticeTreeItem newCurrentTreeItem) {
97110
currentTreeItem = newCurrentTreeItem;
98111
}
99112

113+
/**
114+
* Pack directory
115+
*/
116+
private void pack(File directory, String toSave) throws IOException {
117+
URI root = directory.toURI();
118+
Deque<File> queue = new LinkedList<File>();
119+
queue.push(directory);
120+
OutputStream out = new FileOutputStream(new File(toSave));
121+
Closeable res = out;
122+
try {
123+
ZipOutputStream zout = new ZipOutputStream(out);
124+
res = zout;
125+
while(!queue.isEmpty()) {
126+
directory = queue.pop();
127+
for(File child : directory.listFiles()) {
128+
String name = root.relativize(child.toURI()).getPath();
129+
if(child.isDirectory()) {
130+
queue.push(child);
131+
name = name.endsWith("/") ? name : (name + "/");
132+
zout.putNextEntry(new ZipEntry(name));
133+
} else {
134+
zout.putNextEntry(new ZipEntry(name));
135+
InputStream in = new FileInputStream(child);
136+
try {
137+
byte[] buffer = new byte[1024];
138+
while(true) {
139+
int readCount = in.read(buffer);
140+
if(readCount<0) break;
141+
zout.write(buffer, 0, readCount);
142+
}
143+
} finally {
144+
in.close();
145+
}
146+
zout.closeEntry();
147+
}
148+
}
149+
}
150+
} finally {
151+
res.close();
152+
}
153+
}
154+
100155
/**
101156
* Write node
102157
*/
@@ -115,6 +170,29 @@ public void writeNode(NoticeCategory node, String name) throws IOException {
115170
}
116171
}
117172

173+
/**
174+
* Write node in filesystem
175+
*/
176+
private void writeFSNode(NoticeCategory node, String name, File dir) throws IOException {
177+
System.out.println("In " + node.getName() + " with name " + name);
178+
if(node.getSubCategories()!=null) {
179+
for(NoticeCategory cat : node.getSubCategories()) {
180+
File newDir = new File(dir.getPath() + "/" + name);
181+
if(newDir.exists()) newDir.delete();
182+
newDir.mkdir();
183+
writeFSNode(cat, cat.getName(), newDir);
184+
}
185+
}
186+
else {
187+
File toWrite = new File(dir.getPath() + "/" + name);
188+
if(!toWrite.exists()) toWrite.createNewFile();
189+
FileWriter writer = new FileWriter(toWrite);
190+
writer.write(node.getContent());
191+
writer.close();
192+
}
193+
System.out.println("Exit");
194+
}
195+
118196
/**
119197
* Rebuild tree
120198
*/
@@ -245,6 +323,21 @@ else if(source.equals(exportHTMLItem)) {
245323
} catch(IOException ioe) {
246324
}
247325
}
326+
else if(source.equals(zipItem)) {
327+
try {
328+
File toWrite;
329+
if(openedFile!=null) toWrite = new File(openedFile.getParent() + "/." + ((NoticeTreeItem)noticeTree.getRoot()).getNotice().getName());
330+
else toWrite = chooser.showSaveDialog(main.getPrimaryStage());
331+
if(toWrite.exists()) toWrite.delete();
332+
toWrite.mkdir();
333+
writeFSNode(((NoticeTreeItem)noticeTree.getRoot()).getNotice(), ((NoticeTreeItem)noticeTree.getRoot()).getNotice().getName(), toWrite);
334+
System.out.println("1");
335+
pack(toWrite, (openedFile.getParent() + "/" + ((NoticeTreeItem)noticeTree.getRoot()).getNotice().getName() + ".zip"));
336+
System.out.println("2");
337+
toWrite.delete();
338+
} catch(IOException ioe) {
339+
}
340+
}
248341
else if(source.equals(exitItem)) Platform.exit();
249342
}
250343

0 commit comments

Comments
 (0)